wcd938x.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/device.h>
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/component.h>
  12. #include <sound/soc.h>
  13. #include <sound/tlv.h>
  14. #include <soc/soundwire.h>
  15. #include <linux/regmap.h>
  16. #include <sound/soc.h>
  17. #include <sound/soc-dapm.h>
  18. #include <asoc/wcdcal-hwdep.h>
  19. #include <asoc/msm-cdc-pinctrl.h>
  20. #include <asoc/msm-cdc-supply.h>
  21. #include <dt-bindings/sound/audio-codec-port-types.h>
  22. #include "internal.h"
  23. #include "wcd938x-registers.h"
  24. #define WCD938X_DRV_NAME "wcd938x_codec"
  25. #define NUM_SWRS_DT_PARAMS 5
  26. #define WCD938X_VARIANT_ENTRY_SIZE 32
  27. #define WCD938X_VERSION_1_0 1
  28. #define WCD938X_VERSION_ENTRY_SIZE 32
  29. #define EAR_RX_PATH_AUX 1
  30. #define ADC_MODE_VAL_HIFI 0x01
  31. #define ADC_MODE_VAL_LO_HIF 0x02
  32. #define ADC_MODE_VAL_NORMAL 0x03
  33. #define ADC_MODE_VAL_LP 0x05
  34. #define ADC_MODE_VAL_ULP1 0x09
  35. #define ADC_MODE_VAL_ULP2 0x0B
  36. enum {
  37. WCD9380 = 0,
  38. WCD9385 = 5,
  39. };
  40. enum {
  41. CODEC_TX = 0,
  42. CODEC_RX,
  43. };
  44. enum {
  45. WCD_ADC1 = 0,
  46. WCD_ADC2,
  47. WCD_ADC3,
  48. WCD_ADC4,
  49. ALLOW_BUCK_DISABLE,
  50. HPH_COMP_DELAY,
  51. HPH_PA_DELAY,
  52. };
  53. enum {
  54. ADC_MODE_INVALID = 0,
  55. ADC_MODE_HIFI,
  56. ADC_MODE_LO_HIF,
  57. ADC_MODE_NORMAL,
  58. ADC_MODE_LP,
  59. ADC_MODE_ULP1,
  60. ADC_MODE_ULP2,
  61. };
  62. static const DECLARE_TLV_DB_SCALE(line_gain, 0, 7, 1);
  63. static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 25, 1);
  64. static int wcd938x_handle_post_irq(void *data);
  65. static int wcd938x_reset(struct device *dev);
  66. static int wcd938x_reset_low(struct device *dev);
  67. static const struct regmap_irq wcd938x_irqs[WCD938X_NUM_IRQS] = {
  68. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_BUTTON_PRESS_DET, 0, 0x01),
  69. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_BUTTON_RELEASE_DET, 0, 0x02),
  70. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_ELECT_INS_REM_DET, 0, 0x04),
  71. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_ELECT_INS_REM_LEG_DET, 0, 0x08),
  72. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_SW_DET, 0, 0x10),
  73. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_OCP_INT, 0, 0x20),
  74. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_CNP_INT, 0, 0x40),
  75. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_OCP_INT, 0, 0x80),
  76. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_CNP_INT, 1, 0x01),
  77. REGMAP_IRQ_REG(WCD938X_IRQ_EAR_CNP_INT, 1, 0x02),
  78. REGMAP_IRQ_REG(WCD938X_IRQ_EAR_SCD_INT, 1, 0x04),
  79. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_CNP_INT, 1, 0x08),
  80. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_SCD_INT, 1, 0x10),
  81. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_PDM_WD_INT, 1, 0x20),
  82. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_PDM_WD_INT, 1, 0x40),
  83. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_PDM_WD_INT, 1, 0x80),
  84. REGMAP_IRQ_REG(WCD938X_IRQ_LDORT_SCD_INT, 2, 0x01),
  85. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_MOISTURE_INT, 2, 0x02),
  86. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_SURGE_DET_INT, 2, 0x04),
  87. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_SURGE_DET_INT, 2, 0x08),
  88. };
  89. static struct regmap_irq_chip wcd938x_regmap_irq_chip = {
  90. .name = "wcd938x",
  91. .irqs = wcd938x_irqs,
  92. .num_irqs = ARRAY_SIZE(wcd938x_irqs),
  93. .num_regs = 3,
  94. .status_base = WCD938X_DIGITAL_INTR_STATUS_0,
  95. .mask_base = WCD938X_DIGITAL_INTR_MASK_0,
  96. .type_base = WCD938X_DIGITAL_INTR_LEVEL_0,
  97. .ack_base = WCD938X_DIGITAL_INTR_CLEAR_0,
  98. .use_ack = 1,
  99. .runtime_pm = false,
  100. .handle_post_irq = wcd938x_handle_post_irq,
  101. .irq_drv_data = NULL,
  102. };
  103. static int wcd938x_handle_post_irq(void *data)
  104. {
  105. struct wcd938x_priv *wcd938x = data;
  106. u32 sts1 = 0, sts2 = 0, sts3 = 0;
  107. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_0, &sts1);
  108. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_1, &sts2);
  109. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_2, &sts3);
  110. wcd938x->tx_swr_dev->slave_irq_pending =
  111. ((sts1 || sts2 || sts3) ? true : false);
  112. return IRQ_HANDLED;
  113. }
  114. static int wcd938x_init_reg(struct snd_soc_component *component)
  115. {
  116. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x0E, 0x0E);
  117. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x80, 0x80);
  118. /* 1 msec delay as per HW requirement */
  119. usleep_range(1000, 1010);
  120. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x40, 0x40);
  121. /* 1 msec delay as per HW requirement */
  122. usleep_range(1000, 1010);
  123. snd_soc_component_update_bits(component, WCD938X_LDORXTX_CONFIG,
  124. 0x10, 0x00);
  125. snd_soc_component_update_bits(component, WCD938X_BIAS_VBG_FINE_ADJ,
  126. 0xF0, 0x80);
  127. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x80, 0x80);
  128. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x40, 0x40);
  129. /* 10 msec delay as per HW requirement */
  130. usleep_range(10000, 10010);
  131. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x40, 0x00);
  132. snd_soc_component_update_bits(component, WCD938X_HPH_OCP_CTL,
  133. 0xFF, 0x3A);
  134. snd_soc_component_update_bits(component, WCD938X_RX_OCP_CTL,
  135. 0x0F, 0x02);
  136. snd_soc_component_update_bits(component, WCD938X_HPH_R_TEST,
  137. 0x01, 0x01);
  138. snd_soc_component_update_bits(component, WCD938X_HPH_L_TEST,
  139. 0x01, 0x01);
  140. snd_soc_component_update_bits(component,
  141. WCD938X_HPH_NEW_INT_RDAC_GAIN_CTL,
  142. 0xF0, 0x00);
  143. snd_soc_component_update_bits(component,
  144. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_L_NEW,
  145. 0x1F, 0x15);
  146. snd_soc_component_update_bits(component,
  147. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R_NEW,
  148. 0x1F, 0x15);
  149. snd_soc_component_update_bits(component, WCD938X_HPH_REFBUFF_UHQA_CTL,
  150. 0xC0, 0x80);
  151. snd_soc_component_update_bits(component, WCD938X_DIGITAL_CDC_DMIC_CTL,
  152. 0x02, 0x02);
  153. snd_soc_component_update_bits(component,
  154. WCD938X_TX_COM_NEW_INT_TXFE_ICTRL_STG2CASC_ULP,
  155. 0xFF, 0x14);
  156. snd_soc_component_update_bits(component,
  157. WCD938X_TX_COM_NEW_INT_TXFE_ICTRL_STG2MAIN_ULP,
  158. 0x1F, 0x08);
  159. snd_soc_component_update_bits(component,
  160. WCD938X_DIGITAL_TX_REQ_FB_CTL_0, 0xFF, 0x55);
  161. snd_soc_component_update_bits(component,
  162. WCD938X_DIGITAL_TX_REQ_FB_CTL_1, 0xFF, 0x44);
  163. snd_soc_component_update_bits(component,
  164. WCD938X_DIGITAL_TX_REQ_FB_CTL_2, 0xFF, 0x11);
  165. snd_soc_component_update_bits(component,
  166. WCD938X_DIGITAL_TX_REQ_FB_CTL_3, 0xFF, 0x00);
  167. snd_soc_component_update_bits(component,
  168. WCD938X_DIGITAL_TX_REQ_FB_CTL_4, 0xFF, 0x00);
  169. return 0;
  170. }
  171. static int wcd938x_set_port_params(struct snd_soc_component *component,
  172. u8 slv_prt_type, u8 *port_id, u8 *num_ch,
  173. u8 *ch_mask, u32 *ch_rate,
  174. u8 *port_type, u8 path)
  175. {
  176. int i, j;
  177. u8 num_ports = 0;
  178. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT];
  179. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  180. switch (path) {
  181. case CODEC_RX:
  182. map = &wcd938x->rx_port_mapping;
  183. num_ports = wcd938x->num_rx_ports;
  184. break;
  185. case CODEC_TX:
  186. map = &wcd938x->tx_port_mapping;
  187. num_ports = wcd938x->num_tx_ports;
  188. break;
  189. default:
  190. dev_err(component->dev, "%s Invalid path selected %u\n",
  191. __func__, path);
  192. return -EINVAL;
  193. }
  194. for (i = 0; i <= num_ports; i++) {
  195. for (j = 0; j < MAX_CH_PER_PORT; j++) {
  196. if ((*map)[i][j].slave_port_type == slv_prt_type)
  197. goto found;
  198. }
  199. }
  200. found:
  201. if (i > num_ports || j == MAX_CH_PER_PORT) {
  202. dev_err(component->dev, "%s Failed to find slave port for type %u\n",
  203. __func__, slv_prt_type);
  204. return -EINVAL;
  205. }
  206. *port_id = i;
  207. *num_ch = (*map)[i][j].num_ch;
  208. *ch_mask = (*map)[i][j].ch_mask;
  209. *ch_rate = (*map)[i][j].ch_rate;
  210. *port_type = (*map)[i][j].master_port_type;
  211. return 0;
  212. }
  213. static int wcd938x_parse_port_mapping(struct device *dev,
  214. char *prop, u8 path)
  215. {
  216. u32 *dt_array, map_size, map_length;
  217. u32 port_num = 0, ch_mask, ch_rate, old_port_num = 0;
  218. u32 slave_port_type, master_port_type;
  219. u32 i, ch_iter = 0;
  220. int ret = 0;
  221. u8 *num_ports = NULL;
  222. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT];
  223. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  224. switch (path) {
  225. case CODEC_RX:
  226. map = &wcd938x->rx_port_mapping;
  227. num_ports = &wcd938x->num_rx_ports;
  228. break;
  229. case CODEC_TX:
  230. map = &wcd938x->tx_port_mapping;
  231. num_ports = &wcd938x->num_tx_ports;
  232. break;
  233. default:
  234. dev_err(dev, "%s Invalid path selected %u\n",
  235. __func__, path);
  236. return -EINVAL;
  237. }
  238. if (!of_find_property(dev->of_node, prop,
  239. &map_size)) {
  240. dev_err(dev, "missing port mapping prop %s\n", prop);
  241. ret = -EINVAL;
  242. goto err_port_map;
  243. }
  244. map_length = map_size / (NUM_SWRS_DT_PARAMS * sizeof(u32));
  245. dt_array = kzalloc(map_size, GFP_KERNEL);
  246. if (!dt_array) {
  247. ret = -ENOMEM;
  248. goto err_alloc;
  249. }
  250. ret = of_property_read_u32_array(dev->of_node, prop, dt_array,
  251. NUM_SWRS_DT_PARAMS * map_length);
  252. if (ret) {
  253. dev_err(dev, "%s: Failed to read port mapping from prop %s\n",
  254. __func__, prop);
  255. goto err_pdata_fail;
  256. }
  257. for (i = 0; i < map_length; i++) {
  258. port_num = dt_array[NUM_SWRS_DT_PARAMS * i];
  259. slave_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 1];
  260. ch_mask = dt_array[NUM_SWRS_DT_PARAMS * i + 2];
  261. ch_rate = dt_array[NUM_SWRS_DT_PARAMS * i + 3];
  262. master_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 4];
  263. if (port_num != old_port_num)
  264. ch_iter = 0;
  265. (*map)[port_num][ch_iter].slave_port_type = slave_port_type;
  266. (*map)[port_num][ch_iter].ch_mask = ch_mask;
  267. (*map)[port_num][ch_iter].master_port_type = master_port_type;
  268. (*map)[port_num][ch_iter].num_ch = __sw_hweight8(ch_mask);
  269. (*map)[port_num][ch_iter++].ch_rate = ch_rate;
  270. old_port_num = port_num;
  271. }
  272. *num_ports = port_num;
  273. kfree(dt_array);
  274. return 0;
  275. err_pdata_fail:
  276. kfree(dt_array);
  277. err_alloc:
  278. err_port_map:
  279. return ret;
  280. }
  281. static int wcd938x_tx_connect_port(struct snd_soc_component *component,
  282. u8 slv_port_type, u8 enable)
  283. {
  284. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  285. u8 port_id, num_ch, ch_mask, port_type;
  286. u32 ch_rate;
  287. u8 num_port = 1;
  288. int ret = 0;
  289. ret = wcd938x_set_port_params(component, slv_port_type, &port_id,
  290. &num_ch, &ch_mask, &ch_rate,
  291. &port_type, CODEC_TX);
  292. if (ret)
  293. return ret;
  294. if (enable)
  295. ret = swr_connect_port(wcd938x->tx_swr_dev, &port_id,
  296. num_port, &ch_mask, &ch_rate,
  297. &num_ch, &port_type);
  298. else
  299. ret = swr_disconnect_port(wcd938x->tx_swr_dev, &port_id,
  300. num_port, &ch_mask, &port_type);
  301. return ret;
  302. }
  303. static int wcd938x_rx_connect_port(struct snd_soc_component *component,
  304. u8 slv_port_type, u8 enable)
  305. {
  306. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  307. u8 port_id, num_ch, ch_mask, port_type;
  308. u32 ch_rate;
  309. u8 num_port = 1;
  310. int ret = 0;
  311. ret = wcd938x_set_port_params(component, slv_port_type, &port_id,
  312. &num_ch, &ch_mask, &ch_rate,
  313. &port_type, CODEC_RX);
  314. if (ret)
  315. return ret;
  316. if (enable)
  317. ret = swr_connect_port(wcd938x->rx_swr_dev, &port_id,
  318. num_port, &ch_mask, &ch_rate,
  319. &num_ch, &port_type);
  320. else
  321. ret = swr_disconnect_port(wcd938x->rx_swr_dev, &port_id,
  322. num_port, &ch_mask, &port_type);
  323. return ret;
  324. }
  325. static int wcd938x_rx_clk_enable(struct snd_soc_component *component)
  326. {
  327. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  328. if (wcd938x->rx_clk_cnt == 0) {
  329. snd_soc_component_update_bits(component,
  330. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x01, 0x01);
  331. snd_soc_component_update_bits(component,
  332. WCD938X_ANA_RX_SUPPLIES, 0x01, 0x01);
  333. snd_soc_component_update_bits(component,
  334. WCD938X_DIGITAL_CDC_RX0_CTL, 0x40, 0x00);
  335. snd_soc_component_update_bits(component,
  336. WCD938X_DIGITAL_CDC_RX1_CTL, 0x40, 0x00);
  337. snd_soc_component_update_bits(component,
  338. WCD938X_DIGITAL_CDC_RX2_CTL, 0x40, 0x00);
  339. snd_soc_component_update_bits(component,
  340. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x02, 0x02);
  341. snd_soc_component_update_bits(component,
  342. WCD938X_AUX_AUXPA, 0x10, 0x10);
  343. }
  344. wcd938x->rx_clk_cnt++;
  345. return 0;
  346. }
  347. static int wcd938x_rx_clk_disable(struct snd_soc_component *component)
  348. {
  349. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  350. wcd938x->rx_clk_cnt--;
  351. if (wcd938x->rx_clk_cnt == 0) {
  352. snd_soc_component_update_bits(component,
  353. WCD938X_ANA_RX_SUPPLIES, 0x40, 0x00);
  354. snd_soc_component_update_bits(component,
  355. WCD938X_ANA_RX_SUPPLIES, 0x80, 0x00);
  356. snd_soc_component_update_bits(component,
  357. WCD938X_ANA_RX_SUPPLIES, 0x01, 0x00);
  358. snd_soc_component_update_bits(component,
  359. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x02, 0x00);
  360. snd_soc_component_update_bits(component,
  361. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x01, 0x00);
  362. }
  363. return 0;
  364. }
  365. /*
  366. * wcd938x_soc_get_mbhc: get wcd938x_mbhc handle of corresponding component
  367. * @component: handle to snd_soc_component *
  368. *
  369. * return wcd938x_mbhc handle or error code in case of failure
  370. */
  371. struct wcd938x_mbhc *wcd938x_soc_get_mbhc(struct snd_soc_component *component)
  372. {
  373. struct wcd938x_priv *wcd938x;
  374. if (!component) {
  375. pr_err("%s: Invalid params, NULL component\n", __func__);
  376. return NULL;
  377. }
  378. wcd938x = snd_soc_component_get_drvdata(component);
  379. if (!wcd938x) {
  380. pr_err("%s: wcd938x is NULL\n", __func__);
  381. return NULL;
  382. }
  383. return wcd938x->mbhc;
  384. }
  385. EXPORT_SYMBOL(wcd938x_soc_get_mbhc);
  386. static int wcd938x_codec_hphl_dac_event(struct snd_soc_dapm_widget *w,
  387. struct snd_kcontrol *kcontrol,
  388. int event)
  389. {
  390. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  391. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  392. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  393. w->name, event);
  394. switch (event) {
  395. case SND_SOC_DAPM_PRE_PMU:
  396. wcd938x_rx_clk_enable(component);
  397. snd_soc_component_update_bits(component,
  398. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x01, 0x01);
  399. snd_soc_component_update_bits(component,
  400. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x04, 0x04);
  401. snd_soc_component_update_bits(component,
  402. WCD938X_HPH_RDAC_CLK_CTL1, 0x80, 0x00);
  403. break;
  404. case SND_SOC_DAPM_POST_PMU:
  405. snd_soc_component_update_bits(component,
  406. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_L, 0x0F, 0x02);
  407. if (wcd938x->comp1_enable) {
  408. snd_soc_component_update_bits(component,
  409. WCD938X_DIGITAL_CDC_COMP_CTL_0, 0x02, 0x02);
  410. /* 5msec compander delay as per HW requirement */
  411. if (!wcd938x->comp2_enable ||
  412. (snd_soc_component_read32(component,
  413. WCD938X_DIGITAL_CDC_COMP_CTL_0) & 0x01))
  414. usleep_range(5000, 5010);
  415. snd_soc_component_update_bits(component,
  416. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x00);
  417. } else {
  418. snd_soc_component_update_bits(component,
  419. WCD938X_DIGITAL_CDC_COMP_CTL_0,
  420. 0x02, 0x00);
  421. snd_soc_component_update_bits(component,
  422. WCD938X_HPH_L_EN, 0x20, 0x20);
  423. }
  424. break;
  425. case SND_SOC_DAPM_POST_PMD:
  426. snd_soc_component_update_bits(component,
  427. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R,
  428. 0x0F, 0x01);
  429. break;
  430. }
  431. return 0;
  432. }
  433. static int wcd938x_codec_hphr_dac_event(struct snd_soc_dapm_widget *w,
  434. struct snd_kcontrol *kcontrol,
  435. int event)
  436. {
  437. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  438. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  439. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  440. w->name, event);
  441. switch (event) {
  442. case SND_SOC_DAPM_PRE_PMU:
  443. wcd938x_rx_clk_enable(component);
  444. snd_soc_component_update_bits(component,
  445. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x02, 0x02);
  446. snd_soc_component_update_bits(component,
  447. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x08, 0x08);
  448. snd_soc_component_update_bits(component,
  449. WCD938X_HPH_RDAC_CLK_CTL1, 0x80, 0x00);
  450. break;
  451. case SND_SOC_DAPM_POST_PMU:
  452. snd_soc_component_update_bits(component,
  453. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R, 0x0F, 0x02);
  454. if (wcd938x->comp2_enable) {
  455. snd_soc_component_update_bits(component,
  456. WCD938X_DIGITAL_CDC_COMP_CTL_0, 0x01, 0x01);
  457. /* 5msec compander delay as per HW requirement */
  458. if (!wcd938x->comp1_enable ||
  459. (snd_soc_component_read32(component,
  460. WCD938X_DIGITAL_CDC_COMP_CTL_0) & 0x02))
  461. usleep_range(5000, 5010);
  462. snd_soc_component_update_bits(component,
  463. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x00);
  464. } else {
  465. snd_soc_component_update_bits(component,
  466. WCD938X_DIGITAL_CDC_COMP_CTL_0,
  467. 0x01, 0x00);
  468. snd_soc_component_update_bits(component,
  469. WCD938X_HPH_R_EN, 0x20, 0x20);
  470. }
  471. break;
  472. case SND_SOC_DAPM_POST_PMD:
  473. snd_soc_component_update_bits(component,
  474. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R,
  475. 0x0F, 0x01);
  476. break;
  477. }
  478. return 0;
  479. }
  480. static int wcd938x_codec_ear_dac_event(struct snd_soc_dapm_widget *w,
  481. struct snd_kcontrol *kcontrol,
  482. int event)
  483. {
  484. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  485. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  486. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  487. w->name, event);
  488. switch (event) {
  489. case SND_SOC_DAPM_PRE_PMU:
  490. wcd938x_rx_clk_enable(component);
  491. snd_soc_component_update_bits(component,
  492. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x04, 0x04);
  493. snd_soc_component_update_bits(component,
  494. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x01, 0x01);
  495. snd_soc_component_update_bits(component,
  496. WCD938X_DIGITAL_CDC_COMP_CTL_0, 0x02, 0x02);
  497. /* 5 msec delay as per HW requirement */
  498. usleep_range(5000, 5010);
  499. if (wcd938x->flyback_cur_det_disable == 0)
  500. snd_soc_component_update_bits(component,
  501. WCD938X_FLYBACK_EN,
  502. 0x04, 0x00);
  503. wcd938x->flyback_cur_det_disable++;
  504. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  505. WCD_CLSH_EVENT_PRE_DAC,
  506. WCD_CLSH_STATE_EAR,
  507. wcd938x->hph_mode);
  508. break;
  509. case SND_SOC_DAPM_POST_PMD:
  510. break;
  511. };
  512. return 0;
  513. }
  514. static int wcd938x_codec_aux_dac_event(struct snd_soc_dapm_widget *w,
  515. struct snd_kcontrol *kcontrol,
  516. int event)
  517. {
  518. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  519. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  520. int ret = 0;
  521. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  522. w->name, event);
  523. switch (event) {
  524. case SND_SOC_DAPM_PRE_PMU:
  525. wcd938x_rx_clk_enable(component);
  526. snd_soc_component_update_bits(component,
  527. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x04, 0x04);
  528. snd_soc_component_update_bits(component,
  529. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x04, 0x04);
  530. snd_soc_component_update_bits(component,
  531. WCD938X_DIGITAL_CDC_AUX_GAIN_CTL, 0x01, 0x01);
  532. if (wcd938x->flyback_cur_det_disable == 0)
  533. snd_soc_component_update_bits(component,
  534. WCD938X_FLYBACK_EN,
  535. 0x04, 0x00);
  536. wcd938x->flyback_cur_det_disable++;
  537. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  538. WCD_CLSH_EVENT_PRE_DAC,
  539. WCD_CLSH_STATE_AUX,
  540. wcd938x->hph_mode);
  541. break;
  542. case SND_SOC_DAPM_POST_PMD:
  543. snd_soc_component_update_bits(component,
  544. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x04, 0x00);
  545. break;
  546. };
  547. return ret;
  548. }
  549. static int wcd938x_codec_enable_hphr_pa(struct snd_soc_dapm_widget *w,
  550. struct snd_kcontrol *kcontrol,
  551. int event)
  552. {
  553. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  554. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  555. int ret = 0;
  556. int hph_mode = wcd938x->hph_mode;
  557. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  558. w->name, event);
  559. switch (event) {
  560. case SND_SOC_DAPM_PRE_PMU:
  561. if (wcd938x->update_wcd_event)
  562. wcd938x->update_wcd_event(wcd938x->handle,
  563. WCD_BOLERO_EVT_RX_MUTE,
  564. (WCD_RX2 << 0x10 | 0x1));
  565. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  566. wcd938x->rx_swr_dev->dev_num,
  567. true);
  568. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  569. WCD_CLSH_EVENT_PRE_DAC,
  570. WCD_CLSH_STATE_HPHR,
  571. hph_mode);
  572. wcd_clsh_set_hph_mode(component, CLS_H_HIFI);
  573. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  574. 0x10, 0x10);
  575. wcd_clsh_set_hph_mode(component, hph_mode);
  576. /* 100 usec delay as per HW requirement */
  577. usleep_range(100, 110);
  578. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  579. snd_soc_component_update_bits(component,
  580. WCD938X_DIGITAL_PDM_WD_CTL1, 0x17, 0x13);
  581. break;
  582. case SND_SOC_DAPM_POST_PMU:
  583. /*
  584. * 7ms sleep is required if compander is enabled as per
  585. * HW requirement. If compander is disabled, then
  586. * 20ms delay is required.
  587. */
  588. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  589. if (!wcd938x->comp2_enable)
  590. usleep_range(20000, 20100);
  591. else
  592. usleep_range(7000, 7100);
  593. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  594. }
  595. snd_soc_component_update_bits(component,
  596. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x02);
  597. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  598. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  599. snd_soc_component_update_bits(component,
  600. WCD938X_ANA_RX_SUPPLIES, 0x02, 0x02);
  601. if (wcd938x->update_wcd_event)
  602. wcd938x->update_wcd_event(wcd938x->handle,
  603. WCD_BOLERO_EVT_RX_MUTE,
  604. (WCD_RX2 << 0x10));
  605. wcd_enable_irq(&wcd938x->irq_info,
  606. WCD938X_IRQ_HPHR_PDM_WD_INT);
  607. break;
  608. case SND_SOC_DAPM_PRE_PMD:
  609. wcd_disable_irq(&wcd938x->irq_info,
  610. WCD938X_IRQ_HPHR_PDM_WD_INT);
  611. if (wcd938x->update_wcd_event)
  612. wcd938x->update_wcd_event(wcd938x->handle,
  613. WCD_BOLERO_EVT_RX_MUTE,
  614. (WCD_RX2 << 0x10 | 0x1));
  615. if (wcd938x->update_wcd_event)
  616. wcd938x->update_wcd_event(wcd938x->handle,
  617. WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST,
  618. (WCD_RX2 << 0x10));
  619. /* 7 msec delay as per HW requirement */
  620. usleep_range(7000, 7100);
  621. if (wcd938x->update_wcd_event)
  622. wcd938x->update_wcd_event(wcd938x->handle,
  623. WCD_BOLERO_EVT_RX_MUTE,
  624. (WCD_RX2 << 0x10 | 0x0));
  625. /* 20 msec delay as per HW requirement */
  626. usleep_range(21000, 21100);
  627. if (wcd938x->update_wcd_event)
  628. wcd938x->update_wcd_event(wcd938x->handle,
  629. WCD_BOLERO_EVT_RX_MUTE,
  630. (WCD_RX2 << 0x10 | 0x1));
  631. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  632. 0x40, 0x00);
  633. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  634. WCD_EVENT_PRE_HPHR_PA_OFF,
  635. &wcd938x->mbhc->wcd_mbhc);
  636. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  637. break;
  638. case SND_SOC_DAPM_POST_PMD:
  639. /*
  640. * 7ms sleep is required if compander is enabled as per
  641. * HW requirement. If compander is disabled, then
  642. * 20ms delay is required.
  643. */
  644. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  645. if (!wcd938x->comp2_enable)
  646. usleep_range(20000, 20100);
  647. else
  648. usleep_range(7000, 7100);
  649. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  650. }
  651. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  652. WCD_EVENT_POST_HPHR_PA_OFF,
  653. &wcd938x->mbhc->wcd_mbhc);
  654. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  655. 0x10, 0x00);
  656. /* 20 msec delay as per HW requirement */
  657. usleep_range(20000, 20100);
  658. snd_soc_component_update_bits(component,
  659. WCD938X_DIGITAL_PDM_WD_CTL1, 0x17, 0x00);
  660. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  661. WCD_CLSH_EVENT_POST_PA,
  662. WCD_CLSH_STATE_HPHR,
  663. hph_mode);
  664. break;
  665. };
  666. return ret;
  667. }
  668. static int wcd938x_codec_enable_hphl_pa(struct snd_soc_dapm_widget *w,
  669. struct snd_kcontrol *kcontrol,
  670. int event)
  671. {
  672. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  673. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  674. int ret = 0;
  675. int hph_mode = wcd938x->hph_mode;
  676. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  677. w->name, event);
  678. switch (event) {
  679. case SND_SOC_DAPM_PRE_PMU:
  680. if (wcd938x->update_wcd_event)
  681. wcd938x->update_wcd_event(wcd938x->handle,
  682. WCD_BOLERO_EVT_RX_MUTE,
  683. (WCD_RX1 << 0x10 | 0x01));
  684. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  685. wcd938x->rx_swr_dev->dev_num,
  686. true);
  687. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  688. WCD_CLSH_EVENT_PRE_DAC,
  689. WCD_CLSH_STATE_HPHL,
  690. hph_mode);
  691. wcd_clsh_set_hph_mode(component, CLS_H_HIFI);
  692. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  693. 0x20, 0x20);
  694. wcd_clsh_set_hph_mode(component, hph_mode);
  695. /* 100 usec delay as per HW requirement */
  696. usleep_range(100, 110);
  697. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  698. snd_soc_component_update_bits(component,
  699. WCD938X_DIGITAL_PDM_WD_CTL0, 0x17, 0x13);
  700. break;
  701. case SND_SOC_DAPM_POST_PMU:
  702. /*
  703. * 7ms sleep is required if compander is enabled as per
  704. * HW requirement. If compander is disabled, then
  705. * 20ms delay is required.
  706. */
  707. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  708. if (!wcd938x->comp1_enable)
  709. usleep_range(20000, 20100);
  710. else
  711. usleep_range(7000, 7100);
  712. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  713. }
  714. snd_soc_component_update_bits(component,
  715. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x02);
  716. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  717. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  718. snd_soc_component_update_bits(component,
  719. WCD938X_ANA_RX_SUPPLIES, 0x02, 0x02);
  720. if (wcd938x->update_wcd_event)
  721. wcd938x->update_wcd_event(wcd938x->handle,
  722. WCD_BOLERO_EVT_RX_MUTE,
  723. (WCD_RX1 << 0x10));
  724. wcd_enable_irq(&wcd938x->irq_info,
  725. WCD938X_IRQ_HPHL_PDM_WD_INT);
  726. break;
  727. case SND_SOC_DAPM_PRE_PMD:
  728. wcd_disable_irq(&wcd938x->irq_info,
  729. WCD938X_IRQ_HPHL_PDM_WD_INT);
  730. if (wcd938x->update_wcd_event)
  731. wcd938x->update_wcd_event(wcd938x->handle,
  732. WCD_BOLERO_EVT_RX_MUTE,
  733. (WCD_RX1 << 0x10 | 0x1));
  734. if (wcd938x->update_wcd_event)
  735. wcd938x->update_wcd_event(wcd938x->handle,
  736. WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST,
  737. (WCD_RX1 << 0x10));
  738. /* 7 msec delay as per HW requirement */
  739. usleep_range(7000, 7100);
  740. if (wcd938x->update_wcd_event)
  741. wcd938x->update_wcd_event(wcd938x->handle,
  742. WCD_BOLERO_EVT_RX_MUTE,
  743. (WCD_RX1 << 0x10 | 0x0));
  744. /* 20 msec delay as per HW requirement */
  745. usleep_range(21000, 21100);
  746. if (wcd938x->update_wcd_event)
  747. wcd938x->update_wcd_event(wcd938x->handle,
  748. WCD_BOLERO_EVT_RX_MUTE,
  749. (WCD_RX1 << 0x10 | 0x1));
  750. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  751. 0x80, 0x00);
  752. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  753. WCD_EVENT_PRE_HPHL_PA_OFF,
  754. &wcd938x->mbhc->wcd_mbhc);
  755. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  756. break;
  757. case SND_SOC_DAPM_POST_PMD:
  758. /*
  759. * 7ms sleep is required if compander is enabled as per
  760. * HW requirement. If compander is disabled, then
  761. * 20ms delay is required.
  762. */
  763. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  764. if (!wcd938x->comp1_enable)
  765. usleep_range(21000, 21100);
  766. else
  767. usleep_range(7000, 7100);
  768. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  769. }
  770. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  771. WCD_EVENT_POST_HPHL_PA_OFF,
  772. &wcd938x->mbhc->wcd_mbhc);
  773. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  774. 0x20, 0x00);
  775. /* 20 msec delay as per HW requirement */
  776. usleep_range(21000, 21100);
  777. snd_soc_component_update_bits(component,
  778. WCD938X_DIGITAL_PDM_WD_CTL0, 0x17, 0x00);
  779. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  780. WCD_CLSH_EVENT_POST_PA,
  781. WCD_CLSH_STATE_HPHL,
  782. hph_mode);
  783. break;
  784. };
  785. return ret;
  786. }
  787. static int wcd938x_codec_enable_aux_pa(struct snd_soc_dapm_widget *w,
  788. struct snd_kcontrol *kcontrol,
  789. int event)
  790. {
  791. struct snd_soc_component *component =
  792. snd_soc_dapm_to_component(w->dapm);
  793. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  794. int hph_mode = wcd938x->hph_mode;
  795. int ret = 0;
  796. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  797. w->name, event);
  798. switch (event) {
  799. case SND_SOC_DAPM_PRE_PMU:
  800. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  801. wcd938x->rx_swr_dev->dev_num,
  802. true);
  803. snd_soc_component_update_bits(component,
  804. WCD938X_DIGITAL_PDM_WD_CTL2, 0x05, 0x05);
  805. break;
  806. case SND_SOC_DAPM_POST_PMU:
  807. /* 1 msec delay as per HW requirement */
  808. usleep_range(1000, 1010);
  809. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  810. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  811. snd_soc_component_update_bits(component,
  812. WCD938X_ANA_RX_SUPPLIES,
  813. 0x02, 0x02);
  814. if (wcd938x->update_wcd_event)
  815. wcd938x->update_wcd_event(wcd938x->handle,
  816. WCD_BOLERO_EVT_RX_MUTE,
  817. (WCD_RX3 << 0x10));
  818. wcd_enable_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT);
  819. break;
  820. case SND_SOC_DAPM_PRE_PMD:
  821. wcd_disable_irq(&wcd938x->irq_info,
  822. WCD938X_IRQ_AUX_PDM_WD_INT);
  823. if (wcd938x->update_wcd_event)
  824. wcd938x->update_wcd_event(wcd938x->handle,
  825. WCD_BOLERO_EVT_RX_MUTE,
  826. (WCD_RX3 << 0x10 | 0x1));
  827. break;
  828. case SND_SOC_DAPM_POST_PMD:
  829. /* 1 msec delay as per HW requirement */
  830. usleep_range(1000, 1010);
  831. snd_soc_component_update_bits(component,
  832. WCD938X_DIGITAL_PDM_WD_CTL2, 0x05, 0x00);
  833. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  834. WCD_CLSH_EVENT_POST_PA,
  835. WCD_CLSH_STATE_AUX,
  836. hph_mode);
  837. wcd938x->flyback_cur_det_disable--;
  838. if (wcd938x->flyback_cur_det_disable == 0)
  839. snd_soc_component_update_bits(component,
  840. WCD938X_FLYBACK_EN,
  841. 0x04, 0x04);
  842. break;
  843. };
  844. return ret;
  845. }
  846. static int wcd938x_codec_enable_ear_pa(struct snd_soc_dapm_widget *w,
  847. struct snd_kcontrol *kcontrol,
  848. int event)
  849. {
  850. struct snd_soc_component *component =
  851. snd_soc_dapm_to_component(w->dapm);
  852. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  853. int hph_mode = wcd938x->hph_mode;
  854. int ret = 0;
  855. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  856. w->name, event);
  857. switch (event) {
  858. case SND_SOC_DAPM_PRE_PMU:
  859. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  860. wcd938x->rx_swr_dev->dev_num,
  861. true);
  862. /*
  863. * Enable watchdog interrupt for HPHL or AUX
  864. * depending on mux value
  865. */
  866. wcd938x->ear_rx_path =
  867. snd_soc_component_read32(
  868. component, WCD938X_DIGITAL_CDC_EAR_PATH_CTL);
  869. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX)
  870. snd_soc_component_update_bits(component,
  871. WCD938X_DIGITAL_PDM_WD_CTL2,
  872. 0x05, 0x05);
  873. else
  874. snd_soc_component_update_bits(component,
  875. WCD938X_DIGITAL_PDM_WD_CTL0,
  876. 0x17, 0x13);
  877. break;
  878. case SND_SOC_DAPM_POST_PMU:
  879. /* 6 msec delay as per HW requirement */
  880. usleep_range(6000, 6010);
  881. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  882. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  883. snd_soc_component_update_bits(component,
  884. WCD938X_ANA_RX_SUPPLIES,
  885. 0x02, 0x02);
  886. if (wcd938x->update_wcd_event)
  887. wcd938x->update_wcd_event(wcd938x->handle,
  888. WCD_BOLERO_EVT_RX_MUTE,
  889. (WCD_RX1 << 0x10));
  890. break;
  891. case SND_SOC_DAPM_PRE_PMD:
  892. if (wcd938x->update_wcd_event)
  893. wcd938x->update_wcd_event(wcd938x->handle,
  894. WCD_BOLERO_EVT_RX_MUTE,
  895. (WCD_RX1 << 0x10 | 0x1));
  896. break;
  897. case SND_SOC_DAPM_POST_PMD:
  898. /* 7 msec delay as per HW requirement */
  899. usleep_range(7000, 7010);
  900. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX)
  901. snd_soc_component_update_bits(component,
  902. WCD938X_DIGITAL_PDM_WD_CTL2,
  903. 0x05, 0x00);
  904. else
  905. snd_soc_component_update_bits(component,
  906. WCD938X_DIGITAL_PDM_WD_CTL0,
  907. 0x17, 0x00);
  908. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  909. WCD_CLSH_EVENT_POST_PA,
  910. WCD_CLSH_STATE_EAR,
  911. hph_mode);
  912. wcd938x->flyback_cur_det_disable--;
  913. if (wcd938x->flyback_cur_det_disable == 0)
  914. snd_soc_component_update_bits(component,
  915. WCD938X_FLYBACK_EN,
  916. 0x04, 0x04);
  917. break;
  918. };
  919. return ret;
  920. }
  921. static int wcd938x_enable_clsh(struct snd_soc_dapm_widget *w,
  922. struct snd_kcontrol *kcontrol,
  923. int event)
  924. {
  925. struct snd_soc_component *component =
  926. snd_soc_dapm_to_component(w->dapm);
  927. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  928. int mode = wcd938x->hph_mode;
  929. int ret = 0;
  930. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  931. w->name, event);
  932. if (mode == CLS_H_LOHIFI || mode == CLS_H_ULP ||
  933. mode == CLS_H_HIFI || mode == CLS_H_LP) {
  934. wcd938x_rx_connect_port(component, CLSH,
  935. SND_SOC_DAPM_EVENT_ON(event));
  936. }
  937. if (SND_SOC_DAPM_EVENT_OFF(event))
  938. ret = swr_slvdev_datapath_control(
  939. wcd938x->rx_swr_dev,
  940. wcd938x->rx_swr_dev->dev_num,
  941. false);
  942. return ret;
  943. }
  944. static int wcd938x_enable_rx1(struct snd_soc_dapm_widget *w,
  945. struct snd_kcontrol *kcontrol,
  946. int event)
  947. {
  948. struct snd_soc_component *component =
  949. snd_soc_dapm_to_component(w->dapm);
  950. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  951. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  952. w->name, event);
  953. switch (event) {
  954. case SND_SOC_DAPM_PRE_PMU:
  955. wcd938x_rx_connect_port(component, HPH_L, true);
  956. if (wcd938x->comp1_enable)
  957. wcd938x_rx_connect_port(component, COMP_L, true);
  958. break;
  959. case SND_SOC_DAPM_POST_PMD:
  960. wcd938x_rx_connect_port(component, HPH_L, false);
  961. if (wcd938x->comp1_enable)
  962. wcd938x_rx_connect_port(component, COMP_L, false);
  963. wcd938x_rx_clk_disable(component);
  964. snd_soc_component_update_bits(component,
  965. WCD938X_DIGITAL_CDC_DIG_CLK_CTL,
  966. 0x01, 0x00);
  967. break;
  968. };
  969. return 0;
  970. }
  971. static int wcd938x_enable_rx2(struct snd_soc_dapm_widget *w,
  972. struct snd_kcontrol *kcontrol, int event)
  973. {
  974. struct snd_soc_component *component =
  975. snd_soc_dapm_to_component(w->dapm);
  976. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  977. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  978. w->name, event);
  979. switch (event) {
  980. case SND_SOC_DAPM_PRE_PMU:
  981. wcd938x_rx_connect_port(component, HPH_R, true);
  982. if (wcd938x->comp2_enable)
  983. wcd938x_rx_connect_port(component, COMP_R, true);
  984. break;
  985. case SND_SOC_DAPM_POST_PMD:
  986. wcd938x_rx_connect_port(component, HPH_R, false);
  987. if (wcd938x->comp2_enable)
  988. wcd938x_rx_connect_port(component, COMP_R, false);
  989. wcd938x_rx_clk_disable(component);
  990. snd_soc_component_update_bits(component,
  991. WCD938X_DIGITAL_CDC_DIG_CLK_CTL,
  992. 0x02, 0x00);
  993. break;
  994. };
  995. return 0;
  996. }
  997. static int wcd938x_enable_rx3(struct snd_soc_dapm_widget *w,
  998. struct snd_kcontrol *kcontrol,
  999. int event)
  1000. {
  1001. struct snd_soc_component *component =
  1002. snd_soc_dapm_to_component(w->dapm);
  1003. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1004. w->name, event);
  1005. switch (event) {
  1006. case SND_SOC_DAPM_PRE_PMU:
  1007. wcd938x_rx_connect_port(component, LO, true);
  1008. break;
  1009. case SND_SOC_DAPM_POST_PMD:
  1010. wcd938x_rx_connect_port(component, LO, false);
  1011. /* 6 msec delay as per HW requirement */
  1012. usleep_range(6000, 6010);
  1013. wcd938x_rx_clk_disable(component);
  1014. snd_soc_component_update_bits(component,
  1015. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x04, 0x00);
  1016. break;
  1017. }
  1018. return 0;
  1019. }
  1020. static int wcd938x_codec_enable_dmic(struct snd_soc_dapm_widget *w,
  1021. struct snd_kcontrol *kcontrol,
  1022. int event)
  1023. {
  1024. struct snd_soc_component *component =
  1025. snd_soc_dapm_to_component(w->dapm);
  1026. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1027. u16 dmic_clk_reg, dmic_clk_en_reg;
  1028. s32 *dmic_clk_cnt;
  1029. u8 dmic_ctl_shift = 0;
  1030. u8 dmic_clk_shift = 0;
  1031. u8 dmic_clk_mask = 0;
  1032. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1033. w->name, event);
  1034. switch (w->shift) {
  1035. case 0:
  1036. case 1:
  1037. dmic_clk_cnt = &(wcd938x->dmic_0_1_clk_cnt);
  1038. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_1_2;
  1039. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC1_CTL;
  1040. dmic_clk_mask = 0x0F;
  1041. dmic_clk_shift = 0x00;
  1042. dmic_ctl_shift = 0x00;
  1043. break;
  1044. case 2:
  1045. case 3:
  1046. dmic_clk_cnt = &(wcd938x->dmic_2_3_clk_cnt);
  1047. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_1_2;
  1048. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC2_CTL;
  1049. dmic_clk_mask = 0xF0;
  1050. dmic_clk_shift = 0x04;
  1051. dmic_ctl_shift = 0x01;
  1052. break;
  1053. case 4:
  1054. case 5:
  1055. dmic_clk_cnt = &(wcd938x->dmic_4_5_clk_cnt);
  1056. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_3_4;
  1057. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC3_CTL;
  1058. dmic_clk_mask = 0x0F;
  1059. dmic_clk_shift = 0x00;
  1060. dmic_ctl_shift = 0x02;
  1061. break;
  1062. case 6:
  1063. case 7:
  1064. dmic_clk_cnt = &(wcd938x->dmic_6_7_clk_cnt);
  1065. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_3_4;
  1066. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC4_CTL;
  1067. dmic_clk_mask = 0xF0;
  1068. dmic_clk_shift = 0x04;
  1069. dmic_ctl_shift = 0x03;
  1070. break;
  1071. default:
  1072. dev_err(component->dev, "%s: Invalid DMIC Selection\n",
  1073. __func__);
  1074. return -EINVAL;
  1075. };
  1076. dev_dbg(component->dev, "%s: event %d DMIC%d dmic_clk_cnt %d\n",
  1077. __func__, event, (w->shift +1), *dmic_clk_cnt);
  1078. switch (event) {
  1079. case SND_SOC_DAPM_PRE_PMU:
  1080. snd_soc_component_update_bits(component,
  1081. WCD938X_DIGITAL_CDC_AMIC_CTL,
  1082. (0x01 << dmic_ctl_shift), 0x00);
  1083. /* 250us sleep as per HW requirement */
  1084. usleep_range(250, 260);
  1085. /* Setting DMIC clock rate to 2.4MHz */
  1086. snd_soc_component_update_bits(component,
  1087. dmic_clk_reg, dmic_clk_mask,
  1088. (0x03 << dmic_clk_shift));
  1089. snd_soc_component_update_bits(component,
  1090. dmic_clk_en_reg, 0x08, 0x08);
  1091. /* enable clock scaling */
  1092. snd_soc_component_update_bits(component,
  1093. WCD938X_DIGITAL_CDC_DMIC_CTL, 0x06, 0x06);
  1094. wcd938x_tx_connect_port(component, DMIC0 + (w->shift), true);
  1095. break;
  1096. case SND_SOC_DAPM_POST_PMD:
  1097. wcd938x_tx_connect_port(component, DMIC0 + (w->shift), false);
  1098. snd_soc_component_update_bits(component,
  1099. WCD938X_DIGITAL_CDC_AMIC_CTL,
  1100. (0x01 << dmic_ctl_shift),
  1101. (0x01 << dmic_ctl_shift));
  1102. snd_soc_component_update_bits(component,
  1103. dmic_clk_en_reg, 0x08, 0x00);
  1104. break;
  1105. };
  1106. return 0;
  1107. }
  1108. /*
  1109. * wcd938x_get_micb_vout_ctl_val: converts micbias from volts to register value
  1110. * @micb_mv: micbias in mv
  1111. *
  1112. * return register value converted
  1113. */
  1114. int wcd938x_get_micb_vout_ctl_val(u32 micb_mv)
  1115. {
  1116. /* min micbias voltage is 1V and maximum is 2.85V */
  1117. if (micb_mv < 1000 || micb_mv > 2850) {
  1118. pr_err("%s: unsupported micbias voltage\n", __func__);
  1119. return -EINVAL;
  1120. }
  1121. return (micb_mv - 1000) / 50;
  1122. }
  1123. EXPORT_SYMBOL(wcd938x_get_micb_vout_ctl_val);
  1124. /*
  1125. * wcd938x_mbhc_micb_adjust_voltage: adjust specific micbias voltage
  1126. * @component: handle to snd_soc_component *
  1127. * @req_volt: micbias voltage to be set
  1128. * @micb_num: micbias to be set, e.g. micbias1 or micbias2
  1129. *
  1130. * return 0 if adjustment is success or error code in case of failure
  1131. */
  1132. int wcd938x_mbhc_micb_adjust_voltage(struct snd_soc_component *component,
  1133. int req_volt, int micb_num)
  1134. {
  1135. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1136. int cur_vout_ctl, req_vout_ctl;
  1137. int micb_reg, micb_val, micb_en;
  1138. int ret = 0;
  1139. switch (micb_num) {
  1140. case MIC_BIAS_1:
  1141. micb_reg = WCD938X_ANA_MICB1;
  1142. break;
  1143. case MIC_BIAS_2:
  1144. micb_reg = WCD938X_ANA_MICB2;
  1145. break;
  1146. case MIC_BIAS_3:
  1147. micb_reg = WCD938X_ANA_MICB3;
  1148. break;
  1149. case MIC_BIAS_4:
  1150. micb_reg = WCD938X_ANA_MICB4;
  1151. break;
  1152. default:
  1153. return -EINVAL;
  1154. }
  1155. mutex_lock(&wcd938x->micb_lock);
  1156. /*
  1157. * If requested micbias voltage is same as current micbias
  1158. * voltage, then just return. Otherwise, adjust voltage as
  1159. * per requested value. If micbias is already enabled, then
  1160. * to avoid slow micbias ramp-up or down enable pull-up
  1161. * momentarily, change the micbias value and then re-enable
  1162. * micbias.
  1163. */
  1164. micb_val = snd_soc_component_read32(component, micb_reg);
  1165. micb_en = (micb_val & 0xC0) >> 6;
  1166. cur_vout_ctl = micb_val & 0x3F;
  1167. req_vout_ctl = wcd938x_get_micb_vout_ctl_val(req_volt);
  1168. if (req_vout_ctl < 0) {
  1169. ret = -EINVAL;
  1170. goto exit;
  1171. }
  1172. if (cur_vout_ctl == req_vout_ctl) {
  1173. ret = 0;
  1174. goto exit;
  1175. }
  1176. dev_dbg(component->dev, "%s: micb_num: %d, cur_mv: %d, req_mv: %d, micb_en: %d\n",
  1177. __func__, micb_num, WCD_VOUT_CTL_TO_MICB(cur_vout_ctl),
  1178. req_volt, micb_en);
  1179. if (micb_en == 0x1)
  1180. snd_soc_component_update_bits(component, micb_reg, 0xC0, 0x80);
  1181. snd_soc_component_update_bits(component, micb_reg, 0x3F, req_vout_ctl);
  1182. if (micb_en == 0x1) {
  1183. snd_soc_component_update_bits(component, micb_reg, 0xC0, 0x40);
  1184. /*
  1185. * Add 2ms delay as per HW requirement after enabling
  1186. * micbias
  1187. */
  1188. usleep_range(2000, 2100);
  1189. }
  1190. exit:
  1191. mutex_unlock(&wcd938x->micb_lock);
  1192. return ret;
  1193. }
  1194. EXPORT_SYMBOL(wcd938x_mbhc_micb_adjust_voltage);
  1195. static int wcd938x_tx_swr_ctrl(struct snd_soc_dapm_widget *w,
  1196. struct snd_kcontrol *kcontrol,
  1197. int event)
  1198. {
  1199. struct snd_soc_component *component =
  1200. snd_soc_dapm_to_component(w->dapm);
  1201. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1202. int ret = 0;
  1203. switch (event) {
  1204. case SND_SOC_DAPM_PRE_PMU:
  1205. ret = swr_slvdev_datapath_control(wcd938x->tx_swr_dev,
  1206. wcd938x->tx_swr_dev->dev_num,
  1207. true);
  1208. break;
  1209. case SND_SOC_DAPM_POST_PMD:
  1210. ret = swr_slvdev_datapath_control(wcd938x->tx_swr_dev,
  1211. wcd938x->tx_swr_dev->dev_num,
  1212. false);
  1213. break;
  1214. };
  1215. return ret;
  1216. }
  1217. static int wcd938x_get_adc_mode(int val)
  1218. {
  1219. int ret = 0;
  1220. switch (val) {
  1221. case ADC_MODE_INVALID:
  1222. ret = ADC_MODE_VAL_NORMAL;
  1223. break;
  1224. case ADC_MODE_HIFI:
  1225. ret = ADC_MODE_VAL_HIFI;
  1226. break;
  1227. case ADC_MODE_LO_HIF:
  1228. ret = ADC_MODE_VAL_LO_HIF;
  1229. break;
  1230. case ADC_MODE_NORMAL:
  1231. ret = ADC_MODE_VAL_NORMAL;
  1232. break;
  1233. case ADC_MODE_LP:
  1234. ret = ADC_MODE_VAL_LP;
  1235. break;
  1236. case ADC_MODE_ULP1:
  1237. ret = ADC_MODE_VAL_ULP1;
  1238. break;
  1239. case ADC_MODE_ULP2:
  1240. ret = ADC_MODE_VAL_ULP2;
  1241. break;
  1242. default:
  1243. ret = -EINVAL;
  1244. pr_err("%s: invalid ADC mode value %d\n", __func__, val);
  1245. break;
  1246. }
  1247. return ret;
  1248. }
  1249. static int wcd938x_codec_enable_adc(struct snd_soc_dapm_widget *w,
  1250. struct snd_kcontrol *kcontrol,
  1251. int event){
  1252. int mode;
  1253. struct snd_soc_component *component =
  1254. snd_soc_dapm_to_component(w->dapm);
  1255. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1256. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1257. w->name, event);
  1258. switch (event) {
  1259. case SND_SOC_DAPM_PRE_PMU:
  1260. mode = wcd938x_get_adc_mode(wcd938x->tx_mode[w->shift]);
  1261. if (mode < 0) {
  1262. dev_info(component->dev,
  1263. "%s: invalid mode, setting to normal mode\n",
  1264. __func__);
  1265. mode = ADC_MODE_VAL_NORMAL;
  1266. }
  1267. snd_soc_component_update_bits(component,
  1268. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x80, 0x80);
  1269. snd_soc_component_update_bits(component,
  1270. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x08, 0x08);
  1271. snd_soc_component_update_bits(component,
  1272. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  1273. switch (w->shift) {
  1274. case 0:
  1275. snd_soc_component_update_bits(component,
  1276. WCD938X_DIGITAL_CDC_TX_ANA_MODE_0_1, 0x0F,
  1277. mode);
  1278. break;
  1279. case 1:
  1280. snd_soc_component_update_bits(component,
  1281. WCD938X_DIGITAL_CDC_TX_ANA_MODE_0_1, 0xF0,
  1282. mode << 4);
  1283. break;
  1284. case 2:
  1285. snd_soc_component_update_bits(component,
  1286. WCD938X_DIGITAL_CDC_TX_ANA_MODE_2_3, 0x0F,
  1287. mode);
  1288. break;
  1289. case 3:
  1290. snd_soc_component_update_bits(component,
  1291. WCD938X_DIGITAL_CDC_TX_ANA_MODE_2_3, 0xF0,
  1292. mode << 4);
  1293. break;
  1294. default:
  1295. break;
  1296. }
  1297. set_bit(w->shift, &wcd938x->status_mask);
  1298. wcd938x_tx_connect_port(component, ADC1 + (w->shift), true);
  1299. break;
  1300. case SND_SOC_DAPM_POST_PMD:
  1301. wcd938x_tx_connect_port(component, ADC1 + (w->shift), false);
  1302. snd_soc_component_update_bits(component,
  1303. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x08, 0x00);
  1304. clear_bit(w->shift, &wcd938x->status_mask);
  1305. break;
  1306. };
  1307. return 0;
  1308. }
  1309. int wcd938x_tx_channel_config(struct snd_soc_component *component,
  1310. int channel, int mode)
  1311. {
  1312. int reg = WCD938X_ANA_TX_CH2, mask = 0, val = 0;
  1313. int ret = 0;
  1314. switch (channel) {
  1315. case 0:
  1316. reg = WCD938X_ANA_TX_CH2;
  1317. mask = 0x40;
  1318. break;
  1319. case 1:
  1320. reg = WCD938X_ANA_TX_CH2;
  1321. mask = 0x20;
  1322. break;
  1323. case 2:
  1324. reg = WCD938X_ANA_TX_CH4;
  1325. mask = 0x40;
  1326. break;
  1327. case 3:
  1328. reg = WCD938X_ANA_TX_CH4;
  1329. mask = 0x20;
  1330. break;
  1331. default:
  1332. pr_err("%s: Invalid channel num %d\n", __func__, channel);
  1333. ret = -EINVAL;
  1334. break;
  1335. }
  1336. if (!mode)
  1337. val = 0x00;
  1338. else
  1339. val = mask;
  1340. if (!ret)
  1341. snd_soc_component_update_bits(component, reg, mask, val);
  1342. return ret;
  1343. }
  1344. static int wcd938x_enable_req(struct snd_soc_dapm_widget *w,
  1345. struct snd_kcontrol *kcontrol, int event)
  1346. {
  1347. struct snd_soc_component *component =
  1348. snd_soc_dapm_to_component(w->dapm);
  1349. int ret = 0;
  1350. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1351. w->name, event);
  1352. switch (event) {
  1353. case SND_SOC_DAPM_PRE_PMU:
  1354. snd_soc_component_update_bits(component,
  1355. WCD938X_DIGITAL_CDC_REQ_CTL, 0x02, 0x02);
  1356. snd_soc_component_update_bits(component,
  1357. WCD938X_DIGITAL_CDC_REQ_CTL, 0x01, 0x00);
  1358. ret = wcd938x_tx_channel_config(component, w->shift, 1);
  1359. snd_soc_component_update_bits(component,
  1360. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x30, 0x30);
  1361. snd_soc_component_update_bits(component,
  1362. WCD938X_ANA_TX_CH1, 0x80, 0x80);
  1363. snd_soc_component_update_bits(component,
  1364. WCD938X_ANA_TX_CH2, 0x80, 0x80);
  1365. ret |= wcd938x_tx_channel_config(component, w->shift, 0);
  1366. break;
  1367. case SND_SOC_DAPM_POST_PMD:
  1368. snd_soc_component_update_bits(component,
  1369. WCD938X_ANA_TX_CH1, 0x80, 0x00);
  1370. snd_soc_component_update_bits(component,
  1371. WCD938X_ANA_TX_CH2, 0x80, 0x00);
  1372. snd_soc_component_update_bits(component,
  1373. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x10, 0x00);
  1374. snd_soc_component_update_bits(component,
  1375. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x00);
  1376. snd_soc_component_update_bits(component,
  1377. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x80, 0x00);
  1378. break;
  1379. };
  1380. return ret;
  1381. }
  1382. int wcd938x_micbias_control(struct snd_soc_component *component,
  1383. int micb_num, int req, bool is_dapm)
  1384. {
  1385. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1386. int micb_index = micb_num - 1;
  1387. u16 micb_reg;
  1388. int pre_off_event = 0, post_off_event = 0;
  1389. int post_on_event = 0, post_dapm_off = 0;
  1390. int post_dapm_on = 0;
  1391. if ((micb_index < 0) || (micb_index > WCD938X_MAX_MICBIAS - 1)) {
  1392. dev_err(component->dev,
  1393. "%s: Invalid micbias index, micb_ind:%d\n",
  1394. __func__, micb_index);
  1395. return -EINVAL;
  1396. }
  1397. if (NULL == wcd938x) {
  1398. dev_err(component->dev,
  1399. "%s: wcd938x private data is NULL\n", __func__);
  1400. return -EINVAL;
  1401. }
  1402. switch (micb_num) {
  1403. case MIC_BIAS_1:
  1404. micb_reg = WCD938X_ANA_MICB1;
  1405. break;
  1406. case MIC_BIAS_2:
  1407. micb_reg = WCD938X_ANA_MICB2;
  1408. pre_off_event = WCD_EVENT_PRE_MICBIAS_2_OFF;
  1409. post_off_event = WCD_EVENT_POST_MICBIAS_2_OFF;
  1410. post_on_event = WCD_EVENT_POST_MICBIAS_2_ON;
  1411. post_dapm_on = WCD_EVENT_POST_DAPM_MICBIAS_2_ON;
  1412. post_dapm_off = WCD_EVENT_POST_DAPM_MICBIAS_2_OFF;
  1413. break;
  1414. case MIC_BIAS_3:
  1415. micb_reg = WCD938X_ANA_MICB3;
  1416. break;
  1417. case MIC_BIAS_4:
  1418. micb_reg = WCD938X_ANA_MICB4;
  1419. break;
  1420. default:
  1421. dev_err(component->dev, "%s: Invalid micbias number: %d\n",
  1422. __func__, micb_num);
  1423. return -EINVAL;
  1424. };
  1425. mutex_lock(&wcd938x->micb_lock);
  1426. switch (req) {
  1427. case MICB_PULLUP_ENABLE:
  1428. wcd938x->pullup_ref[micb_index]++;
  1429. if ((wcd938x->pullup_ref[micb_index] == 1) &&
  1430. (wcd938x->micb_ref[micb_index] == 0))
  1431. snd_soc_component_update_bits(component, micb_reg,
  1432. 0xC0, 0x80);
  1433. break;
  1434. case MICB_PULLUP_DISABLE:
  1435. if (wcd938x->pullup_ref[micb_index] > 0)
  1436. wcd938x->pullup_ref[micb_index]--;
  1437. if ((wcd938x->pullup_ref[micb_index] == 0) &&
  1438. (wcd938x->micb_ref[micb_index] == 0))
  1439. snd_soc_component_update_bits(component, micb_reg,
  1440. 0xC0, 0x00);
  1441. break;
  1442. case MICB_ENABLE:
  1443. wcd938x->micb_ref[micb_index]++;
  1444. if (wcd938x->micb_ref[micb_index] == 1) {
  1445. snd_soc_component_update_bits(component,
  1446. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0xE0, 0xE0);
  1447. snd_soc_component_update_bits(component,
  1448. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  1449. snd_soc_component_update_bits(component,
  1450. WCD938X_DIGITAL_CDC_ANA_TX_CLK_CTL, 0x01, 0x01);
  1451. snd_soc_component_update_bits(component,
  1452. WCD938X_MICB1_TEST_CTL_2, 0x01, 0x01);
  1453. snd_soc_component_update_bits(component,
  1454. WCD938X_MICB2_TEST_CTL_2, 0x01, 0x01);
  1455. snd_soc_component_update_bits(component,
  1456. WCD938X_MICB3_TEST_CTL_2, 0x01, 0x01);
  1457. snd_soc_component_update_bits(component,
  1458. WCD938X_MICB4_TEST_CTL_2, 0x01, 0x01);
  1459. snd_soc_component_update_bits(component,
  1460. micb_reg, 0xC0, 0x40);
  1461. if (post_on_event)
  1462. blocking_notifier_call_chain(
  1463. &wcd938x->mbhc->notifier,
  1464. post_on_event,
  1465. &wcd938x->mbhc->wcd_mbhc);
  1466. }
  1467. if (is_dapm && post_dapm_on && wcd938x->mbhc)
  1468. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  1469. post_dapm_on,
  1470. &wcd938x->mbhc->wcd_mbhc);
  1471. break;
  1472. case MICB_DISABLE:
  1473. if (wcd938x->micb_ref[micb_index] > 0)
  1474. wcd938x->micb_ref[micb_index]--;
  1475. if ((wcd938x->micb_ref[micb_index] == 0) &&
  1476. (wcd938x->pullup_ref[micb_index] > 0))
  1477. snd_soc_component_update_bits(component, micb_reg,
  1478. 0xC0, 0x80);
  1479. else if ((wcd938x->micb_ref[micb_index] == 0) &&
  1480. (wcd938x->pullup_ref[micb_index] == 0)) {
  1481. if (pre_off_event && wcd938x->mbhc)
  1482. blocking_notifier_call_chain(
  1483. &wcd938x->mbhc->notifier,
  1484. pre_off_event,
  1485. &wcd938x->mbhc->wcd_mbhc);
  1486. snd_soc_component_update_bits(component, micb_reg,
  1487. 0xC0, 0x00);
  1488. if (post_off_event && wcd938x->mbhc)
  1489. blocking_notifier_call_chain(
  1490. &wcd938x->mbhc->notifier,
  1491. post_off_event,
  1492. &wcd938x->mbhc->wcd_mbhc);
  1493. }
  1494. if (is_dapm && post_dapm_off && wcd938x->mbhc)
  1495. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  1496. post_dapm_off,
  1497. &wcd938x->mbhc->wcd_mbhc);
  1498. break;
  1499. };
  1500. dev_dbg(component->dev,
  1501. "%s: micb_num:%d, micb_ref: %d, pullup_ref: %d\n",
  1502. __func__, micb_num, wcd938x->micb_ref[micb_index],
  1503. wcd938x->pullup_ref[micb_index]);
  1504. mutex_unlock(&wcd938x->micb_lock);
  1505. return 0;
  1506. }
  1507. EXPORT_SYMBOL(wcd938x_micbias_control);
  1508. static int wcd938x_get_logical_addr(struct swr_device *swr_dev)
  1509. {
  1510. int ret = 0;
  1511. uint8_t devnum = 0;
  1512. ret = swr_get_logical_dev_num(swr_dev, swr_dev->addr, &devnum);
  1513. if (ret) {
  1514. dev_err(&swr_dev->dev,
  1515. "%s get devnum %d for dev addr %lx failed\n",
  1516. __func__, devnum, swr_dev->addr);
  1517. swr_remove_device(swr_dev);
  1518. return ret;
  1519. }
  1520. swr_dev->dev_num = devnum;
  1521. return 0;
  1522. }
  1523. static int wcd938x_event_notify(struct notifier_block *block,
  1524. unsigned long val,
  1525. void *data)
  1526. {
  1527. u16 event = (val & 0xffff);
  1528. int ret = 0;
  1529. struct wcd938x_priv *wcd938x = dev_get_drvdata((struct device *)data);
  1530. struct snd_soc_component *component = wcd938x->component;
  1531. struct wcd_mbhc *mbhc;
  1532. switch (event) {
  1533. case BOLERO_WCD_EVT_TX_CH_HOLD_CLEAR:
  1534. if (test_bit(WCD_ADC1, &wcd938x->status_mask)) {
  1535. snd_soc_component_update_bits(component,
  1536. WCD938X_ANA_TX_CH2, 0x40, 0x00);
  1537. clear_bit(WCD_ADC1, &wcd938x->status_mask);
  1538. }
  1539. if (test_bit(WCD_ADC2, &wcd938x->status_mask)) {
  1540. snd_soc_component_update_bits(component,
  1541. WCD938X_ANA_TX_CH2, 0x20, 0x00);
  1542. clear_bit(WCD_ADC2, &wcd938x->status_mask);
  1543. }
  1544. if (test_bit(WCD_ADC3, &wcd938x->status_mask)) {
  1545. snd_soc_component_update_bits(component,
  1546. WCD938X_ANA_TX_CH4, 0x40, 0x00);
  1547. clear_bit(WCD_ADC3, &wcd938x->status_mask);
  1548. }
  1549. if (test_bit(WCD_ADC4, &wcd938x->status_mask)) {
  1550. snd_soc_component_update_bits(component,
  1551. WCD938X_ANA_TX_CH4, 0x20, 0x00);
  1552. clear_bit(WCD_ADC4, &wcd938x->status_mask);
  1553. }
  1554. break;
  1555. case BOLERO_WCD_EVT_PA_OFF_PRE_SSR:
  1556. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  1557. 0xC0, 0x00);
  1558. snd_soc_component_update_bits(component, WCD938X_ANA_EAR,
  1559. 0x80, 0x00);
  1560. snd_soc_component_update_bits(component, WCD938X_AUX_AUXPA,
  1561. 0x80, 0x00);
  1562. break;
  1563. case BOLERO_WCD_EVT_SSR_DOWN:
  1564. mbhc = &wcd938x->mbhc->wcd_mbhc;
  1565. wcd938x_mbhc_ssr_down(wcd938x->mbhc, component);
  1566. wcd938x_reset_low(wcd938x->dev);
  1567. break;
  1568. case BOLERO_WCD_EVT_SSR_UP:
  1569. wcd938x_reset(wcd938x->dev);
  1570. wcd938x_get_logical_addr(wcd938x->tx_swr_dev);
  1571. wcd938x_get_logical_addr(wcd938x->rx_swr_dev);
  1572. wcd938x_init_reg(component);
  1573. regcache_mark_dirty(wcd938x->regmap);
  1574. regcache_sync(wcd938x->regmap);
  1575. /* Initialize MBHC module */
  1576. mbhc = &wcd938x->mbhc->wcd_mbhc;
  1577. ret = wcd938x_mbhc_post_ssr_init(wcd938x->mbhc, component);
  1578. if (ret) {
  1579. dev_err(component->dev, "%s: mbhc initialization failed\n",
  1580. __func__);
  1581. } else {
  1582. wcd938x_mbhc_hs_detect(component, mbhc->mbhc_cfg);
  1583. }
  1584. break;
  1585. case BOLERO_WCD_EVT_CLK_NOTIFY:
  1586. snd_soc_component_update_bits(component,
  1587. WCD938X_DIGITAL_TOP_CLK_CFG, 0x06,
  1588. ((val >> 0x10) << 0x01));
  1589. break;
  1590. default:
  1591. dev_dbg(component->dev, "%s: invalid event %d\n", __func__, event);
  1592. break;
  1593. }
  1594. return 0;
  1595. }
  1596. static int __wcd938x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1597. int event)
  1598. {
  1599. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  1600. int micb_num;
  1601. dev_dbg(component->dev, "%s: wname: %s, event: %d\n",
  1602. __func__, w->name, event);
  1603. if (strnstr(w->name, "MIC BIAS1", sizeof("MIC BIAS1")))
  1604. micb_num = MIC_BIAS_1;
  1605. else if (strnstr(w->name, "MIC BIAS2", sizeof("MIC BIAS2")))
  1606. micb_num = MIC_BIAS_2;
  1607. else if (strnstr(w->name, "MIC BIAS3", sizeof("MIC BIAS3")))
  1608. micb_num = MIC_BIAS_3;
  1609. else if (strnstr(w->name, "MIC BIAS4", sizeof("MIC BIAS4")))
  1610. micb_num = MIC_BIAS_4;
  1611. else
  1612. return -EINVAL;
  1613. switch (event) {
  1614. case SND_SOC_DAPM_PRE_PMU:
  1615. wcd938x_micbias_control(component, micb_num,
  1616. MICB_ENABLE, true);
  1617. break;
  1618. case SND_SOC_DAPM_POST_PMU:
  1619. /* 1 msec delay as per HW requirement */
  1620. usleep_range(1000, 1100);
  1621. break;
  1622. case SND_SOC_DAPM_POST_PMD:
  1623. wcd938x_micbias_control(component, micb_num,
  1624. MICB_DISABLE, true);
  1625. break;
  1626. };
  1627. return 0;
  1628. }
  1629. static int wcd938x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1630. struct snd_kcontrol *kcontrol,
  1631. int event)
  1632. {
  1633. return __wcd938x_codec_enable_micbias(w, event);
  1634. }
  1635. static inline int wcd938x_tx_path_get(const char *wname,
  1636. unsigned int *path_num)
  1637. {
  1638. int ret = 0;
  1639. char *widget_name = NULL;
  1640. char *w_name = NULL;
  1641. char *path_num_char = NULL;
  1642. char *path_name = NULL;
  1643. widget_name = kstrndup(wname, 9, GFP_KERNEL);
  1644. if (!widget_name)
  1645. return -EINVAL;
  1646. w_name = widget_name;
  1647. path_name = strsep(&widget_name, " ");
  1648. if (!path_name) {
  1649. pr_err("%s: Invalid widget name = %s\n",
  1650. __func__, widget_name);
  1651. ret = -EINVAL;
  1652. goto err;
  1653. }
  1654. path_num_char = strpbrk(path_name, "0123");
  1655. if (!path_num_char) {
  1656. pr_err("%s: tx path index not found\n",
  1657. __func__);
  1658. ret = -EINVAL;
  1659. goto err;
  1660. }
  1661. ret = kstrtouint(path_num_char, 10, path_num);
  1662. if (ret < 0)
  1663. pr_err("%s: Invalid tx path = %s\n",
  1664. __func__, w_name);
  1665. err:
  1666. kfree(w_name);
  1667. return ret;
  1668. }
  1669. static int wcd938x_tx_mode_get(struct snd_kcontrol *kcontrol,
  1670. struct snd_ctl_elem_value *ucontrol)
  1671. {
  1672. struct snd_soc_component *component =
  1673. snd_soc_kcontrol_component(kcontrol);
  1674. struct wcd938x_priv *wcd938x = NULL;
  1675. int ret = 0;
  1676. unsigned int path = 0;
  1677. if (!component)
  1678. return -EINVAL;
  1679. wcd938x = snd_soc_component_get_drvdata(component);
  1680. if (!wcd938x)
  1681. return -EINVAL;
  1682. ret = wcd938x_tx_path_get(kcontrol->id.name, &path);
  1683. if (ret < 0)
  1684. return ret;
  1685. ucontrol->value.integer.value[0] = wcd938x->tx_mode[path];
  1686. return 0;
  1687. }
  1688. static int wcd938x_tx_mode_put(struct snd_kcontrol *kcontrol,
  1689. struct snd_ctl_elem_value *ucontrol)
  1690. {
  1691. struct snd_soc_component *component =
  1692. snd_soc_kcontrol_component(kcontrol);
  1693. struct wcd938x_priv *wcd938x = NULL;
  1694. u32 mode_val;
  1695. unsigned int path = 0;
  1696. int ret = 0;
  1697. if (!component)
  1698. return -EINVAL;
  1699. wcd938x = snd_soc_component_get_drvdata(component);
  1700. if (!wcd938x)
  1701. return -EINVAL;
  1702. ret = wcd938x_tx_path_get(kcontrol->id.name, &path);
  1703. if (ret)
  1704. return ret;
  1705. mode_val = ucontrol->value.enumerated.item[0];
  1706. dev_dbg(component->dev, "%s: mode: %d\n", __func__, mode_val);
  1707. wcd938x->tx_mode[path] = mode_val;
  1708. return 0;
  1709. }
  1710. static int wcd938x_rx_hph_mode_get(struct snd_kcontrol *kcontrol,
  1711. struct snd_ctl_elem_value *ucontrol)
  1712. {
  1713. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  1714. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1715. ucontrol->value.integer.value[0] = wcd938x->hph_mode;
  1716. return 0;
  1717. }
  1718. static int wcd938x_rx_hph_mode_put(struct snd_kcontrol *kcontrol,
  1719. struct snd_ctl_elem_value *ucontrol)
  1720. {
  1721. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  1722. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1723. u32 mode_val;
  1724. mode_val = ucontrol->value.enumerated.item[0];
  1725. dev_dbg(component->dev, "%s: mode: %d\n", __func__, mode_val);
  1726. if (mode_val == 0) {
  1727. dev_info(component->dev,
  1728. "%s:Invalid HPH Mode, default to class_AB\n",
  1729. __func__);
  1730. mode_val = 3; /* enum will be updated later */
  1731. }
  1732. wcd938x->hph_mode = mode_val;
  1733. return 0;
  1734. }
  1735. static int wcd938x_get_compander(struct snd_kcontrol *kcontrol,
  1736. struct snd_ctl_elem_value *ucontrol)
  1737. {
  1738. struct snd_soc_component *component =
  1739. snd_soc_kcontrol_component(kcontrol);
  1740. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1741. bool hphr;
  1742. struct soc_multi_mixer_control *mc;
  1743. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1744. hphr = mc->shift;
  1745. ucontrol->value.integer.value[0] = hphr ? wcd938x->comp2_enable :
  1746. wcd938x->comp1_enable;
  1747. return 0;
  1748. }
  1749. static int wcd938x_set_compander(struct snd_kcontrol *kcontrol,
  1750. struct snd_ctl_elem_value *ucontrol)
  1751. {
  1752. struct snd_soc_component *component =
  1753. snd_soc_kcontrol_component(kcontrol);
  1754. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1755. int value = ucontrol->value.integer.value[0];
  1756. bool hphr;
  1757. struct soc_multi_mixer_control *mc;
  1758. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1759. hphr = mc->shift;
  1760. if (hphr)
  1761. wcd938x->comp2_enable = value;
  1762. else
  1763. wcd938x->comp1_enable = value;
  1764. return 0;
  1765. }
  1766. static const char * const tx_mode_mux_text_wcd9380[] = {
  1767. "ADC_INVALID", "ADC_HIFI", "ADC_LO_HIF", "ADC_NORMAL", "ADC_LP",
  1768. };
  1769. static const struct soc_enum tx_mode_mux_enum_wcd9380 =
  1770. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_mode_mux_text_wcd9380),
  1771. tx_mode_mux_text_wcd9380);
  1772. static const char * const tx_mode_mux_text[] = {
  1773. "ADC_INVALID", "ADC_HIFI", "ADC_LO_HIF", "ADC_NORMAL", "ADC_LP",
  1774. "ADC_ULP1", "ADC_ULP2",
  1775. };
  1776. static const struct soc_enum tx_mode_mux_enum =
  1777. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_mode_mux_text),
  1778. tx_mode_mux_text);
  1779. static const char * const rx_hph_mode_mux_text[] = {
  1780. "CLS_H_INVALID", "CLS_H_HIFI", "CLS_H_LP", "CLS_AB", "CLS_H_LOHIFI",
  1781. "CLS_H_ULP", "CLS_AB_HIFI", "CLS_AB_LP", "CLS_AB_LOHIFI",
  1782. };
  1783. static const struct soc_enum rx_hph_mode_mux_enum =
  1784. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(rx_hph_mode_mux_text),
  1785. rx_hph_mode_mux_text);
  1786. static const struct snd_kcontrol_new wcd9380_snd_controls[] = {
  1787. SOC_ENUM_EXT("TX0 MODE", tx_mode_mux_enum_wcd9380,
  1788. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1789. SOC_ENUM_EXT("TX1 MODE", tx_mode_mux_enum_wcd9380,
  1790. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1791. SOC_ENUM_EXT("TX2 MODE", tx_mode_mux_enum_wcd9380,
  1792. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1793. SOC_ENUM_EXT("TX3 MODE", tx_mode_mux_enum_wcd9380,
  1794. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1795. };
  1796. static const struct snd_kcontrol_new wcd9385_snd_controls[] = {
  1797. SOC_ENUM_EXT("TX0 MODE", tx_mode_mux_enum,
  1798. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1799. SOC_ENUM_EXT("TX1 MODE", tx_mode_mux_enum,
  1800. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1801. SOC_ENUM_EXT("TX2 MODE", tx_mode_mux_enum,
  1802. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1803. SOC_ENUM_EXT("TX3 MODE", tx_mode_mux_enum,
  1804. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1805. };
  1806. static const struct snd_kcontrol_new wcd938x_snd_controls[] = {
  1807. SOC_ENUM_EXT("RX HPH Mode", rx_hph_mode_mux_enum,
  1808. wcd938x_rx_hph_mode_get, wcd938x_rx_hph_mode_put),
  1809. SOC_SINGLE_EXT("HPHL_COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  1810. wcd938x_get_compander, wcd938x_set_compander),
  1811. SOC_SINGLE_EXT("HPHR_COMP Switch", SND_SOC_NOPM, 1, 1, 0,
  1812. wcd938x_get_compander, wcd938x_set_compander),
  1813. SOC_SINGLE_TLV("HPHL Volume", WCD938X_HPH_L_EN, 0, 20, 1, line_gain),
  1814. SOC_SINGLE_TLV("HPHR Volume", WCD938X_HPH_R_EN, 0, 20, 1, line_gain),
  1815. SOC_SINGLE_TLV("ADC1 Volume", WCD938X_ANA_TX_CH1, 0, 20, 0,
  1816. analog_gain),
  1817. SOC_SINGLE_TLV("ADC2 Volume", WCD938X_ANA_TX_CH2, 0, 20, 0,
  1818. analog_gain),
  1819. SOC_SINGLE_TLV("ADC3 Volume", WCD938X_ANA_TX_CH3, 0, 20, 0,
  1820. analog_gain),
  1821. SOC_SINGLE_TLV("ADC4 Volume", WCD938X_ANA_TX_CH4, 0, 20, 0,
  1822. analog_gain),
  1823. };
  1824. static const struct snd_kcontrol_new adc1_switch[] = {
  1825. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1826. };
  1827. static const struct snd_kcontrol_new adc2_switch[] = {
  1828. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1829. };
  1830. static const struct snd_kcontrol_new adc3_switch[] = {
  1831. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1832. };
  1833. static const struct snd_kcontrol_new adc4_switch[] = {
  1834. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1835. };
  1836. static const struct snd_kcontrol_new dmic1_switch[] = {
  1837. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1838. };
  1839. static const struct snd_kcontrol_new dmic2_switch[] = {
  1840. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1841. };
  1842. static const struct snd_kcontrol_new dmic3_switch[] = {
  1843. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1844. };
  1845. static const struct snd_kcontrol_new dmic4_switch[] = {
  1846. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1847. };
  1848. static const struct snd_kcontrol_new dmic5_switch[] = {
  1849. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1850. };
  1851. static const struct snd_kcontrol_new dmic6_switch[] = {
  1852. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1853. };
  1854. static const struct snd_kcontrol_new dmic7_switch[] = {
  1855. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1856. };
  1857. static const struct snd_kcontrol_new dmic8_switch[] = {
  1858. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1859. };
  1860. static const struct snd_kcontrol_new ear_rdac_switch[] = {
  1861. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1862. };
  1863. static const struct snd_kcontrol_new aux_rdac_switch[] = {
  1864. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1865. };
  1866. static const struct snd_kcontrol_new hphl_rdac_switch[] = {
  1867. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1868. };
  1869. static const struct snd_kcontrol_new hphr_rdac_switch[] = {
  1870. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1871. };
  1872. static const char * const adc2_mux_text[] = {
  1873. "INP2", "INP3"
  1874. };
  1875. static const struct soc_enum adc2_enum =
  1876. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 7,
  1877. ARRAY_SIZE(adc2_mux_text), adc2_mux_text);
  1878. static const struct snd_kcontrol_new tx_adc2_mux =
  1879. SOC_DAPM_ENUM("ADC2 MUX Mux", adc2_enum);
  1880. static const char * const adc3_mux_text[] = {
  1881. "INP4", "INP6"
  1882. };
  1883. static const struct soc_enum adc3_enum =
  1884. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 6,
  1885. ARRAY_SIZE(adc3_mux_text), adc3_mux_text);
  1886. static const struct snd_kcontrol_new tx_adc3_mux =
  1887. SOC_DAPM_ENUM("ADC3 MUX Mux", adc3_enum);
  1888. static const char * const adc4_mux_text[] = {
  1889. "INP5", "INP7"
  1890. };
  1891. static const struct soc_enum adc4_enum =
  1892. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 5,
  1893. ARRAY_SIZE(adc4_mux_text), adc4_mux_text);
  1894. static const struct snd_kcontrol_new tx_adc4_mux =
  1895. SOC_DAPM_ENUM("ADC4 MUX Mux", adc4_enum);
  1896. static const char * const rdac3_mux_text[] = {
  1897. "RX1", "RX3"
  1898. };
  1899. static const char * const hdr12_mux_text[] = {
  1900. "NO_HDR12", "HDR12"
  1901. };
  1902. static const struct soc_enum hdr12_enum =
  1903. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 4,
  1904. ARRAY_SIZE(hdr12_mux_text), hdr12_mux_text);
  1905. static const struct snd_kcontrol_new tx_hdr12_mux =
  1906. SOC_DAPM_ENUM("HDR12 MUX Mux", hdr12_enum);
  1907. static const char * const hdr34_mux_text[] = {
  1908. "NO_HDR34", "HDR34"
  1909. };
  1910. static const struct soc_enum hdr34_enum =
  1911. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 3,
  1912. ARRAY_SIZE(hdr34_mux_text), hdr34_mux_text);
  1913. static const struct snd_kcontrol_new tx_hdr34_mux =
  1914. SOC_DAPM_ENUM("HDR34 MUX Mux", hdr34_enum);
  1915. static const struct soc_enum rdac3_enum =
  1916. SOC_ENUM_SINGLE(WCD938X_DIGITAL_CDC_EAR_PATH_CTL, 0,
  1917. ARRAY_SIZE(rdac3_mux_text), rdac3_mux_text);
  1918. static const struct snd_kcontrol_new rx_rdac3_mux =
  1919. SOC_DAPM_ENUM("RDAC3_MUX Mux", rdac3_enum);
  1920. static const struct snd_soc_dapm_widget wcd938x_dapm_widgets[] = {
  1921. /*input widgets*/
  1922. SND_SOC_DAPM_INPUT("AMIC1"),
  1923. SND_SOC_DAPM_INPUT("AMIC2"),
  1924. SND_SOC_DAPM_INPUT("AMIC3"),
  1925. SND_SOC_DAPM_INPUT("AMIC4"),
  1926. SND_SOC_DAPM_INPUT("AMIC5"),
  1927. SND_SOC_DAPM_INPUT("AMIC6"),
  1928. SND_SOC_DAPM_INPUT("AMIC7"),
  1929. SND_SOC_DAPM_INPUT("IN1_HPHL"),
  1930. SND_SOC_DAPM_INPUT("IN2_HPHR"),
  1931. SND_SOC_DAPM_INPUT("IN3_AUX"),
  1932. /*tx widgets*/
  1933. SND_SOC_DAPM_ADC_E("ADC1", NULL, SND_SOC_NOPM, 0, 0,
  1934. wcd938x_codec_enable_adc,
  1935. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1936. SND_SOC_DAPM_ADC_E("ADC2", NULL, SND_SOC_NOPM, 1, 0,
  1937. wcd938x_codec_enable_adc,
  1938. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1939. SND_SOC_DAPM_ADC_E("ADC3", NULL, SND_SOC_NOPM, 2, 0,
  1940. wcd938x_codec_enable_adc,
  1941. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1942. SND_SOC_DAPM_ADC_E("ADC4", NULL, SND_SOC_NOPM, 3, 0,
  1943. wcd938x_codec_enable_adc,
  1944. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1945. SND_SOC_DAPM_ADC_E("DMIC1", NULL, SND_SOC_NOPM, 0, 0,
  1946. wcd938x_codec_enable_dmic,
  1947. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1948. SND_SOC_DAPM_ADC_E("DMIC2", NULL, SND_SOC_NOPM, 1, 0,
  1949. wcd938x_codec_enable_dmic,
  1950. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1951. SND_SOC_DAPM_ADC_E("DMIC3", NULL, SND_SOC_NOPM, 2, 0,
  1952. wcd938x_codec_enable_dmic,
  1953. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1954. SND_SOC_DAPM_ADC_E("DMIC4", NULL, SND_SOC_NOPM, 3, 0,
  1955. wcd938x_codec_enable_dmic,
  1956. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1957. SND_SOC_DAPM_ADC_E("DMIC5", NULL, SND_SOC_NOPM, 4, 0,
  1958. wcd938x_codec_enable_dmic,
  1959. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1960. SND_SOC_DAPM_ADC_E("DMIC6", NULL, SND_SOC_NOPM, 5, 0,
  1961. wcd938x_codec_enable_dmic,
  1962. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1963. SND_SOC_DAPM_ADC_E("DMIC7", NULL, SND_SOC_NOPM, 6, 0,
  1964. wcd938x_codec_enable_dmic,
  1965. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1966. SND_SOC_DAPM_ADC_E("DMIC8", NULL, SND_SOC_NOPM, 7, 0,
  1967. wcd938x_codec_enable_dmic,
  1968. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1969. SND_SOC_DAPM_MIXER_E("ADC1 REQ", SND_SOC_NOPM, 0, 0,
  1970. NULL, 0, wcd938x_enable_req,
  1971. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1972. SND_SOC_DAPM_MIXER_E("ADC2 REQ", SND_SOC_NOPM, 1, 0,
  1973. NULL, 0, wcd938x_enable_req,
  1974. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1975. SND_SOC_DAPM_MIXER_E("ADC3 REQ", SND_SOC_NOPM, 2, 0,
  1976. NULL, 0, wcd938x_enable_req,
  1977. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1978. SND_SOC_DAPM_MIXER_E("ADC4 REQ", SND_SOC_NOPM, 3, 0,
  1979. NULL, 0, wcd938x_enable_req,
  1980. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1981. SND_SOC_DAPM_MUX("ADC2 MUX", SND_SOC_NOPM, 0, 0,
  1982. &tx_adc2_mux),
  1983. SND_SOC_DAPM_MUX("ADC3 MUX", SND_SOC_NOPM, 0, 0,
  1984. &tx_adc3_mux),
  1985. SND_SOC_DAPM_MUX("ADC4 MUX", SND_SOC_NOPM, 0, 0,
  1986. &tx_adc4_mux),
  1987. SND_SOC_DAPM_MUX("HDR12 MUX", SND_SOC_NOPM, 0, 0,
  1988. &tx_hdr12_mux),
  1989. SND_SOC_DAPM_MUX("HDR34 MUX", SND_SOC_NOPM, 0, 0,
  1990. &tx_hdr34_mux),
  1991. /*tx mixers*/
  1992. SND_SOC_DAPM_MIXER_E("ADC1_MIXER", SND_SOC_NOPM, 0, 0,
  1993. adc1_switch, ARRAY_SIZE(adc1_switch),
  1994. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1995. SND_SOC_DAPM_POST_PMD),
  1996. SND_SOC_DAPM_MIXER_E("ADC2_MIXER", SND_SOC_NOPM, 0, 0,
  1997. adc2_switch, ARRAY_SIZE(adc2_switch),
  1998. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1999. SND_SOC_DAPM_POST_PMD),
  2000. SND_SOC_DAPM_MIXER_E("ADC3_MIXER", SND_SOC_NOPM, 0, 0, adc3_switch,
  2001. ARRAY_SIZE(adc3_switch), wcd938x_tx_swr_ctrl,
  2002. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2003. SND_SOC_DAPM_MIXER_E("ADC4_MIXER", SND_SOC_NOPM, 0, 0, adc4_switch,
  2004. ARRAY_SIZE(adc4_switch), wcd938x_tx_swr_ctrl,
  2005. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2006. SND_SOC_DAPM_MIXER_E("DMIC1_MIXER", SND_SOC_NOPM, 0,
  2007. 0, dmic1_switch, ARRAY_SIZE(dmic1_switch),
  2008. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2009. SND_SOC_DAPM_POST_PMD),
  2010. SND_SOC_DAPM_MIXER_E("DMIC2_MIXER", SND_SOC_NOPM, 0,
  2011. 0, dmic2_switch, ARRAY_SIZE(dmic2_switch),
  2012. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2013. SND_SOC_DAPM_POST_PMD),
  2014. SND_SOC_DAPM_MIXER_E("DMIC3_MIXER", SND_SOC_NOPM, 0,
  2015. 0, dmic3_switch, ARRAY_SIZE(dmic3_switch),
  2016. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2017. SND_SOC_DAPM_POST_PMD),
  2018. SND_SOC_DAPM_MIXER_E("DMIC4_MIXER", SND_SOC_NOPM, 0,
  2019. 0, dmic4_switch, ARRAY_SIZE(dmic4_switch),
  2020. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2021. SND_SOC_DAPM_POST_PMD),
  2022. SND_SOC_DAPM_MIXER_E("DMIC5_MIXER", SND_SOC_NOPM, 0,
  2023. 0, dmic5_switch, ARRAY_SIZE(dmic5_switch),
  2024. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2025. SND_SOC_DAPM_POST_PMD),
  2026. SND_SOC_DAPM_MIXER_E("DMIC6_MIXER", SND_SOC_NOPM, 0,
  2027. 0, dmic6_switch, ARRAY_SIZE(dmic6_switch),
  2028. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2029. SND_SOC_DAPM_POST_PMD),
  2030. SND_SOC_DAPM_MIXER_E("DMIC7_MIXER", SND_SOC_NOPM, 0,
  2031. 0, dmic7_switch, ARRAY_SIZE(dmic7_switch),
  2032. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2033. SND_SOC_DAPM_POST_PMD),
  2034. SND_SOC_DAPM_MIXER_E("DMIC8_MIXER", SND_SOC_NOPM, 0,
  2035. 0, dmic8_switch, ARRAY_SIZE(dmic8_switch),
  2036. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2037. SND_SOC_DAPM_POST_PMD),
  2038. /* micbias widgets*/
  2039. SND_SOC_DAPM_MICBIAS_E("MIC BIAS1", SND_SOC_NOPM, 0, 0,
  2040. wcd938x_codec_enable_micbias,
  2041. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2042. SND_SOC_DAPM_POST_PMD),
  2043. SND_SOC_DAPM_MICBIAS_E("MIC BIAS2", SND_SOC_NOPM, 0, 0,
  2044. wcd938x_codec_enable_micbias,
  2045. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2046. SND_SOC_DAPM_POST_PMD),
  2047. SND_SOC_DAPM_MICBIAS_E("MIC BIAS3", SND_SOC_NOPM, 0, 0,
  2048. wcd938x_codec_enable_micbias,
  2049. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2050. SND_SOC_DAPM_POST_PMD),
  2051. SND_SOC_DAPM_MICBIAS_E("MIC BIAS4", SND_SOC_NOPM, 0, 0,
  2052. wcd938x_codec_enable_micbias,
  2053. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2054. SND_SOC_DAPM_POST_PMD),
  2055. SND_SOC_DAPM_SUPPLY_S("CLS_H_PORT", 1, SND_SOC_NOPM, 0, 0,
  2056. wcd938x_enable_clsh,
  2057. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2058. /*rx widgets*/
  2059. SND_SOC_DAPM_PGA_E("EAR PGA", WCD938X_ANA_EAR, 7, 0, NULL, 0,
  2060. wcd938x_codec_enable_ear_pa,
  2061. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2062. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2063. SND_SOC_DAPM_PGA_E("AUX PGA", WCD938X_AUX_AUXPA, 7, 0, NULL, 0,
  2064. wcd938x_codec_enable_aux_pa,
  2065. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2066. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2067. SND_SOC_DAPM_PGA_E("HPHL PGA", WCD938X_ANA_HPH, 7, 0, NULL, 0,
  2068. wcd938x_codec_enable_hphl_pa,
  2069. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2070. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2071. SND_SOC_DAPM_PGA_E("HPHR PGA", WCD938X_ANA_HPH, 6, 0, NULL, 0,
  2072. wcd938x_codec_enable_hphr_pa,
  2073. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2074. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2075. SND_SOC_DAPM_DAC_E("RDAC1", NULL, SND_SOC_NOPM, 0, 0,
  2076. wcd938x_codec_hphl_dac_event,
  2077. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2078. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2079. SND_SOC_DAPM_DAC_E("RDAC2", NULL, SND_SOC_NOPM, 0, 0,
  2080. wcd938x_codec_hphr_dac_event,
  2081. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2082. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2083. SND_SOC_DAPM_DAC_E("RDAC3", NULL, SND_SOC_NOPM, 0, 0,
  2084. wcd938x_codec_ear_dac_event,
  2085. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2086. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2087. SND_SOC_DAPM_DAC_E("RDAC4", NULL, SND_SOC_NOPM, 0, 0,
  2088. wcd938x_codec_aux_dac_event,
  2089. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2090. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2091. SND_SOC_DAPM_MUX("RDAC3_MUX", SND_SOC_NOPM, 0, 0, &rx_rdac3_mux),
  2092. SND_SOC_DAPM_MIXER_E("RX1", SND_SOC_NOPM, 0, 0, NULL, 0,
  2093. wcd938x_enable_rx1, SND_SOC_DAPM_PRE_PMU |
  2094. SND_SOC_DAPM_POST_PMD),
  2095. SND_SOC_DAPM_MIXER_E("RX2", SND_SOC_NOPM, 0, 0, NULL, 0,
  2096. wcd938x_enable_rx2, SND_SOC_DAPM_PRE_PMU |
  2097. SND_SOC_DAPM_POST_PMD),
  2098. SND_SOC_DAPM_MIXER_E("RX3", SND_SOC_NOPM, 0, 0, NULL, 0,
  2099. wcd938x_enable_rx3, SND_SOC_DAPM_PRE_PMU |
  2100. SND_SOC_DAPM_POST_PMD),
  2101. /* rx mixer widgets*/
  2102. SND_SOC_DAPM_MIXER("EAR_RDAC", SND_SOC_NOPM, 0, 0,
  2103. ear_rdac_switch, ARRAY_SIZE(ear_rdac_switch)),
  2104. SND_SOC_DAPM_MIXER("AUX_RDAC", SND_SOC_NOPM, 0, 0,
  2105. aux_rdac_switch, ARRAY_SIZE(aux_rdac_switch)),
  2106. SND_SOC_DAPM_MIXER("HPHL_RDAC", SND_SOC_NOPM, 0, 0,
  2107. hphl_rdac_switch, ARRAY_SIZE(hphl_rdac_switch)),
  2108. SND_SOC_DAPM_MIXER("HPHR_RDAC", SND_SOC_NOPM, 0, 0,
  2109. hphr_rdac_switch, ARRAY_SIZE(hphr_rdac_switch)),
  2110. /*output widgets tx*/
  2111. SND_SOC_DAPM_OUTPUT("ADC1_OUTPUT"),
  2112. SND_SOC_DAPM_OUTPUT("ADC2_OUTPUT"),
  2113. SND_SOC_DAPM_OUTPUT("ADC3_OUTPUT"),
  2114. SND_SOC_DAPM_OUTPUT("ADC4_OUTPUT"),
  2115. SND_SOC_DAPM_OUTPUT("DMIC1_OUTPUT"),
  2116. SND_SOC_DAPM_OUTPUT("DMIC2_OUTPUT"),
  2117. SND_SOC_DAPM_OUTPUT("DMIC3_OUTPUT"),
  2118. SND_SOC_DAPM_OUTPUT("DMIC4_OUTPUT"),
  2119. SND_SOC_DAPM_OUTPUT("DMIC5_OUTPUT"),
  2120. SND_SOC_DAPM_OUTPUT("DMIC6_OUTPUT"),
  2121. SND_SOC_DAPM_OUTPUT("DMIC7_OUTPUT"),
  2122. SND_SOC_DAPM_OUTPUT("DMIC8_OUTPUT"),
  2123. /*output widgets rx*/
  2124. SND_SOC_DAPM_OUTPUT("EAR"),
  2125. SND_SOC_DAPM_OUTPUT("AUX"),
  2126. SND_SOC_DAPM_OUTPUT("HPHL"),
  2127. SND_SOC_DAPM_OUTPUT("HPHR"),
  2128. };
  2129. static const struct snd_soc_dapm_route wcd938x_audio_map[] = {
  2130. {"ADC1_OUTPUT", NULL, "ADC1_MIXER"},
  2131. {"ADC1_MIXER", "Switch", "ADC1 REQ"},
  2132. {"ADC1 REQ", NULL, "ADC1"},
  2133. {"ADC1", NULL, "AMIC1"},
  2134. {"ADC2_OUTPUT", NULL, "ADC2_MIXER"},
  2135. {"ADC2_MIXER", "Switch", "ADC2 REQ"},
  2136. {"ADC2 REQ", NULL, "ADC2"},
  2137. {"ADC2", NULL, "HDR12 MUX"},
  2138. {"HDR12 MUX", "NO_HDR12", "ADC2 MUX"},
  2139. {"HDR12 MUX", "HDR12", "AMIC1"},
  2140. {"ADC2 MUX", "INP3", "AMIC3"},
  2141. {"ADC2 MUX", "INP2", "AMIC2"},
  2142. {"ADC3_OUTPUT", NULL, "ADC3_MIXER"},
  2143. {"ADC3_MIXER", "Switch", "ADC3 REQ"},
  2144. {"ADC3 REQ", NULL, "ADC3"},
  2145. {"ADC3", NULL, "HDR34 MUX"},
  2146. {"HDR34 MUX", "NO_HDR34", "ADC3 MUX"},
  2147. {"HDR34 MUX", "HDR34", "AMIC5"},
  2148. {"ADC3 MUX", "INP4", "AMIC4"},
  2149. {"ADC3 MUX", "INP6", "AMIC6"},
  2150. {"ADC4_OUTPUT", NULL, "ADC4_MIXER"},
  2151. {"ADC4_MIXER", "Switch", "ADC4 REQ"},
  2152. {"ADC4 REQ", NULL, "ADC4"},
  2153. {"ADC4", NULL, "ADC4 MUX"},
  2154. {"ADC4 MUX", "INP5", "AMIC5"},
  2155. {"ADC4 MUX", "INP7", "AMIC7"},
  2156. {"DMIC1_OUTPUT", NULL, "DMIC1_MIXER"},
  2157. {"DMIC1_MIXER", "Switch", "DMIC1"},
  2158. {"DMIC2_OUTPUT", NULL, "DMIC2_MIXER"},
  2159. {"DMIC2_MIXER", "Switch", "DMIC2"},
  2160. {"DMIC3_OUTPUT", NULL, "DMIC3_MIXER"},
  2161. {"DMIC3_MIXER", "Switch", "DMIC3"},
  2162. {"DMIC4_OUTPUT", NULL, "DMIC4_MIXER"},
  2163. {"DMIC4_MIXER", "Switch", "DMIC4"},
  2164. {"DMIC5_OUTPUT", NULL, "DMIC5_MIXER"},
  2165. {"DMIC5_MIXER", "Switch", "DMIC5"},
  2166. {"DMIC6_OUTPUT", NULL, "DMIC6_MIXER"},
  2167. {"DMIC6_MIXER", "Switch", "DMIC6"},
  2168. {"DMIC7_OUTPUT", NULL, "DMIC7_MIXER"},
  2169. {"DMIC7_MIXER", "Switch", "DMIC7"},
  2170. {"DMIC8_OUTPUT", NULL, "DMIC8_MIXER"},
  2171. {"DMIC8_MIXER", "Switch", "DMIC8"},
  2172. {"IN1_HPHL", NULL, "CLS_H_PORT"},
  2173. {"RX1", NULL, "IN1_HPHL"},
  2174. {"RDAC1", NULL, "RX1"},
  2175. {"HPHL_RDAC", "Switch", "RDAC1"},
  2176. {"HPHL PGA", NULL, "HPHL_RDAC"},
  2177. {"HPHL", NULL, "HPHL PGA"},
  2178. {"IN2_HPHR", NULL, "CLS_H_PORT"},
  2179. {"RX2", NULL, "IN2_HPHR"},
  2180. {"RDAC2", NULL, "RX2"},
  2181. {"HPHR_RDAC", "Switch", "RDAC2"},
  2182. {"HPHR PGA", NULL, "HPHR_RDAC"},
  2183. {"HPHR", NULL, "HPHR PGA"},
  2184. {"IN3_AUX", NULL, "CLS_H_PORT"},
  2185. {"RX3", NULL, "IN3_AUX"},
  2186. {"RDAC4", NULL, "RX3"},
  2187. {"AUX_RDAC", "Switch", "RDAC4"},
  2188. {"AUX PGA", NULL, "AUX_RDAC"},
  2189. {"AUX", NULL, "AUX PGA"},
  2190. {"RDAC3_MUX", "RX3", "RX3"},
  2191. {"RDAC3_MUX", "RX1", "RX1"},
  2192. {"RDAC3", NULL, "RDAC3_MUX"},
  2193. {"EAR_RDAC", "Switch", "RDAC3"},
  2194. {"EAR PGA", NULL, "EAR_RDAC"},
  2195. {"EAR", NULL, "EAR PGA"},
  2196. };
  2197. static ssize_t wcd938x_version_read(struct snd_info_entry *entry,
  2198. void *file_private_data,
  2199. struct file *file,
  2200. char __user *buf, size_t count,
  2201. loff_t pos)
  2202. {
  2203. struct wcd938x_priv *priv;
  2204. char buffer[WCD938X_VERSION_ENTRY_SIZE];
  2205. int len = 0;
  2206. priv = (struct wcd938x_priv *) entry->private_data;
  2207. if (!priv) {
  2208. pr_err("%s: wcd938x priv is null\n", __func__);
  2209. return -EINVAL;
  2210. }
  2211. switch (priv->version) {
  2212. case WCD938X_VERSION_1_0:
  2213. len = snprintf(buffer, sizeof(buffer), "WCD938X_1_0\n");
  2214. break;
  2215. default:
  2216. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  2217. }
  2218. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  2219. }
  2220. static struct snd_info_entry_ops wcd938x_info_ops = {
  2221. .read = wcd938x_version_read,
  2222. };
  2223. static ssize_t wcd938x_variant_read(struct snd_info_entry *entry,
  2224. void *file_private_data,
  2225. struct file *file,
  2226. char __user *buf, size_t count,
  2227. loff_t pos)
  2228. {
  2229. struct wcd938x_priv *priv;
  2230. char buffer[WCD938X_VARIANT_ENTRY_SIZE];
  2231. int len = 0;
  2232. priv = (struct wcd938x_priv *) entry->private_data;
  2233. if (!priv) {
  2234. pr_err("%s: wcd938x priv is null\n", __func__);
  2235. return -EINVAL;
  2236. }
  2237. switch (priv->variant) {
  2238. case WCD9380:
  2239. len = snprintf(buffer, sizeof(buffer), "WCD9380\n");
  2240. break;
  2241. case WCD9385:
  2242. len = snprintf(buffer, sizeof(buffer), "WCD9385\n");
  2243. break;
  2244. default:
  2245. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  2246. }
  2247. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  2248. }
  2249. static struct snd_info_entry_ops wcd938x_variant_ops = {
  2250. .read = wcd938x_variant_read,
  2251. };
  2252. /*
  2253. * wcd938x_info_create_codec_entry - creates wcd938x module
  2254. * @codec_root: The parent directory
  2255. * @component: component instance
  2256. *
  2257. * Creates wcd938x module, variant and version entry under the given
  2258. * parent directory.
  2259. *
  2260. * Return: 0 on success or negative error code on failure.
  2261. */
  2262. int wcd938x_info_create_codec_entry(struct snd_info_entry *codec_root,
  2263. struct snd_soc_component *component)
  2264. {
  2265. struct snd_info_entry *version_entry;
  2266. struct snd_info_entry *variant_entry;
  2267. struct wcd938x_priv *priv;
  2268. struct snd_soc_card *card;
  2269. if (!codec_root || !component)
  2270. return -EINVAL;
  2271. priv = snd_soc_component_get_drvdata(component);
  2272. if (priv->entry) {
  2273. dev_dbg(priv->dev,
  2274. "%s:wcd938x module already created\n", __func__);
  2275. return 0;
  2276. }
  2277. card = component->card;
  2278. priv->entry = snd_info_create_subdir(codec_root->module,
  2279. "wcd938x", codec_root);
  2280. if (!priv->entry) {
  2281. dev_dbg(component->dev, "%s: failed to create wcd938x entry\n",
  2282. __func__);
  2283. return -ENOMEM;
  2284. }
  2285. version_entry = snd_info_create_card_entry(card->snd_card,
  2286. "version",
  2287. priv->entry);
  2288. if (!version_entry) {
  2289. dev_dbg(component->dev, "%s: failed to create wcd938x version entry\n",
  2290. __func__);
  2291. return -ENOMEM;
  2292. }
  2293. version_entry->private_data = priv;
  2294. version_entry->size = WCD938X_VERSION_ENTRY_SIZE;
  2295. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  2296. version_entry->c.ops = &wcd938x_info_ops;
  2297. if (snd_info_register(version_entry) < 0) {
  2298. snd_info_free_entry(version_entry);
  2299. return -ENOMEM;
  2300. }
  2301. priv->version_entry = version_entry;
  2302. variant_entry = snd_info_create_card_entry(card->snd_card,
  2303. "variant",
  2304. priv->entry);
  2305. if (!variant_entry) {
  2306. dev_dbg(component->dev, "%s: failed to create wcd938x variant entry\n",
  2307. __func__);
  2308. return -ENOMEM;
  2309. }
  2310. variant_entry->private_data = priv;
  2311. variant_entry->size = WCD938X_VARIANT_ENTRY_SIZE;
  2312. variant_entry->content = SNDRV_INFO_CONTENT_DATA;
  2313. variant_entry->c.ops = &wcd938x_variant_ops;
  2314. if (snd_info_register(variant_entry) < 0) {
  2315. snd_info_free_entry(variant_entry);
  2316. return -ENOMEM;
  2317. }
  2318. priv->variant_entry = variant_entry;
  2319. return 0;
  2320. }
  2321. EXPORT_SYMBOL(wcd938x_info_create_codec_entry);
  2322. static int wcd938x_soc_codec_probe(struct snd_soc_component *component)
  2323. {
  2324. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2325. struct snd_soc_dapm_context *dapm =
  2326. snd_soc_component_get_dapm(component);
  2327. int variant;
  2328. int ret = -EINVAL;
  2329. dev_info(component->dev, "%s()\n", __func__);
  2330. wcd938x = snd_soc_component_get_drvdata(component);
  2331. if (!wcd938x)
  2332. return -EINVAL;
  2333. wcd938x->component = component;
  2334. snd_soc_component_init_regmap(component, wcd938x->regmap);
  2335. variant = (snd_soc_component_read32(component,
  2336. WCD938X_DIGITAL_EFUSE_REG_0) & 0x1E) >> 1;
  2337. wcd938x->variant = variant;
  2338. wcd938x->fw_data = devm_kzalloc(component->dev,
  2339. sizeof(*(wcd938x->fw_data)),
  2340. GFP_KERNEL);
  2341. if (!wcd938x->fw_data) {
  2342. dev_err(component->dev, "Failed to allocate fw_data\n");
  2343. ret = -ENOMEM;
  2344. goto err;
  2345. }
  2346. set_bit(WCD9XXX_MBHC_CAL, wcd938x->fw_data->cal_bit);
  2347. ret = wcd_cal_create_hwdep(wcd938x->fw_data,
  2348. WCD9XXX_CODEC_HWDEP_NODE, component);
  2349. if (ret < 0) {
  2350. dev_err(component->dev, "%s hwdep failed %d\n", __func__, ret);
  2351. goto err_hwdep;
  2352. }
  2353. ret = wcd938x_mbhc_init(&wcd938x->mbhc, component, wcd938x->fw_data);
  2354. if (ret) {
  2355. pr_err("%s: mbhc initialization failed\n", __func__);
  2356. goto err_hwdep;
  2357. }
  2358. snd_soc_dapm_ignore_suspend(dapm, "AMIC1");
  2359. snd_soc_dapm_ignore_suspend(dapm, "AMIC2");
  2360. snd_soc_dapm_ignore_suspend(dapm, "AMIC3");
  2361. snd_soc_dapm_ignore_suspend(dapm, "AMIC4");
  2362. snd_soc_dapm_ignore_suspend(dapm, "AMIC5");
  2363. snd_soc_dapm_ignore_suspend(dapm, "AMIC6");
  2364. snd_soc_dapm_ignore_suspend(dapm, "AMIC7");
  2365. snd_soc_dapm_ignore_suspend(dapm, "DMIC1_OUTPUT");
  2366. snd_soc_dapm_ignore_suspend(dapm, "DMIC2_OUTPUT");
  2367. snd_soc_dapm_ignore_suspend(dapm, "DMIC3_OUTPUT");
  2368. snd_soc_dapm_ignore_suspend(dapm, "DMIC4_OUTPUT");
  2369. snd_soc_dapm_ignore_suspend(dapm, "DMIC5_OUTPUT");
  2370. snd_soc_dapm_ignore_suspend(dapm, "DMIC6_OUTPUT");
  2371. snd_soc_dapm_ignore_suspend(dapm, "DMIC7_OUTPUT");
  2372. snd_soc_dapm_ignore_suspend(dapm, "DMIC8_OUTPUT");
  2373. snd_soc_dapm_ignore_suspend(dapm, "ADC1_OUTPUT");
  2374. snd_soc_dapm_ignore_suspend(dapm, "ADC2_OUTPUT");
  2375. snd_soc_dapm_ignore_suspend(dapm, "ADC3_OUTPUT");
  2376. snd_soc_dapm_ignore_suspend(dapm, "ADC4_OUTPUT");
  2377. snd_soc_dapm_ignore_suspend(dapm, "IN1_HPHL");
  2378. snd_soc_dapm_ignore_suspend(dapm, "IN2_HPHR");
  2379. snd_soc_dapm_ignore_suspend(dapm, "IN3_AUX");
  2380. snd_soc_dapm_ignore_suspend(dapm, "EAR");
  2381. snd_soc_dapm_ignore_suspend(dapm, "AUX");
  2382. snd_soc_dapm_ignore_suspend(dapm, "HPHL");
  2383. snd_soc_dapm_ignore_suspend(dapm, "HPHR");
  2384. snd_soc_dapm_sync(dapm);
  2385. wcd_cls_h_init(&wcd938x->clsh_info);
  2386. wcd938x_init_reg(component);
  2387. if (wcd938x->variant == WCD9380) {
  2388. ret = snd_soc_add_component_controls(component, wcd9380_snd_controls,
  2389. ARRAY_SIZE(wcd9380_snd_controls));
  2390. if (ret < 0) {
  2391. dev_err(component->dev,
  2392. "%s: Failed to add snd ctrls for variant: %d\n",
  2393. __func__, wcd938x->variant);
  2394. goto err_hwdep;
  2395. }
  2396. }
  2397. if (wcd938x->variant == WCD9385) {
  2398. ret = snd_soc_add_component_controls(component, wcd9385_snd_controls,
  2399. ARRAY_SIZE(wcd9385_snd_controls));
  2400. if (ret < 0) {
  2401. dev_err(component->dev,
  2402. "%s: Failed to add snd ctrls for variant: %d\n",
  2403. __func__, wcd938x->variant);
  2404. goto err_hwdep;
  2405. }
  2406. }
  2407. wcd938x->version = WCD938X_VERSION_1_0;
  2408. /* Register event notifier */
  2409. wcd938x->nblock.notifier_call = wcd938x_event_notify;
  2410. if (wcd938x->register_notifier) {
  2411. ret = wcd938x->register_notifier(wcd938x->handle,
  2412. &wcd938x->nblock,
  2413. true);
  2414. if (ret) {
  2415. dev_err(component->dev,
  2416. "%s: Failed to register notifier %d\n",
  2417. __func__, ret);
  2418. return ret;
  2419. }
  2420. }
  2421. return ret;
  2422. err_hwdep:
  2423. wcd938x->fw_data = NULL;
  2424. err:
  2425. return ret;
  2426. }
  2427. static void wcd938x_soc_codec_remove(struct snd_soc_component *component)
  2428. {
  2429. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2430. if (!wcd938x) {
  2431. dev_err(component->dev, "%s: wcd938x is already NULL\n",
  2432. __func__);
  2433. return;
  2434. }
  2435. if (wcd938x->register_notifier)
  2436. wcd938x->register_notifier(wcd938x->handle,
  2437. &wcd938x->nblock,
  2438. false);
  2439. }
  2440. static struct snd_soc_component_driver soc_codec_dev_wcd938x = {
  2441. .name = WCD938X_DRV_NAME,
  2442. .probe = wcd938x_soc_codec_probe,
  2443. .remove = wcd938x_soc_codec_remove,
  2444. .controls = wcd938x_snd_controls,
  2445. .num_controls = ARRAY_SIZE(wcd938x_snd_controls),
  2446. .dapm_widgets = wcd938x_dapm_widgets,
  2447. .num_dapm_widgets = ARRAY_SIZE(wcd938x_dapm_widgets),
  2448. .dapm_routes = wcd938x_audio_map,
  2449. .num_dapm_routes = ARRAY_SIZE(wcd938x_audio_map),
  2450. };
  2451. static int wcd938x_reset(struct device *dev)
  2452. {
  2453. struct wcd938x_priv *wcd938x = NULL;
  2454. int rc = 0;
  2455. int value = 0;
  2456. if (!dev)
  2457. return -ENODEV;
  2458. wcd938x = dev_get_drvdata(dev);
  2459. if (!wcd938x)
  2460. return -EINVAL;
  2461. if (!wcd938x->rst_np) {
  2462. dev_err(dev, "%s: reset gpio device node not specified\n",
  2463. __func__);
  2464. return -EINVAL;
  2465. }
  2466. value = msm_cdc_pinctrl_get_state(wcd938x->rst_np);
  2467. if (value > 0)
  2468. return 0;
  2469. rc = msm_cdc_pinctrl_select_sleep_state(wcd938x->rst_np);
  2470. if (rc) {
  2471. dev_err(dev, "%s: wcd sleep state request fail!\n",
  2472. __func__);
  2473. return rc;
  2474. }
  2475. /* 20us sleep required after pulling the reset gpio to LOW */
  2476. usleep_range(20, 30);
  2477. rc = msm_cdc_pinctrl_select_active_state(wcd938x->rst_np);
  2478. if (rc) {
  2479. dev_err(dev, "%s: wcd active state request fail!\n",
  2480. __func__);
  2481. return rc;
  2482. }
  2483. /* 20us sleep required after pulling the reset gpio to HIGH */
  2484. usleep_range(20, 30);
  2485. return rc;
  2486. }
  2487. static int wcd938x_read_of_property_u32(struct device *dev, const char *name,
  2488. u32 *val)
  2489. {
  2490. int rc = 0;
  2491. rc = of_property_read_u32(dev->of_node, name, val);
  2492. if (rc)
  2493. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  2494. __func__, name, dev->of_node->full_name);
  2495. return rc;
  2496. }
  2497. static void wcd938x_dt_parse_micbias_info(struct device *dev,
  2498. struct wcd938x_micbias_setting *mb)
  2499. {
  2500. u32 prop_val = 0;
  2501. int rc = 0;
  2502. /* MB1 */
  2503. if (of_find_property(dev->of_node, "qcom,cdc-micbias1-mv",
  2504. NULL)) {
  2505. rc = wcd938x_read_of_property_u32(dev,
  2506. "qcom,cdc-micbias1-mv",
  2507. &prop_val);
  2508. if (!rc)
  2509. mb->micb1_mv = prop_val;
  2510. } else {
  2511. dev_info(dev, "%s: Micbias1 DT property not found\n",
  2512. __func__);
  2513. }
  2514. /* MB2 */
  2515. if (of_find_property(dev->of_node, "qcom,cdc-micbias2-mv",
  2516. NULL)) {
  2517. rc = wcd938x_read_of_property_u32(dev,
  2518. "qcom,cdc-micbias2-mv",
  2519. &prop_val);
  2520. if (!rc)
  2521. mb->micb2_mv = prop_val;
  2522. } else {
  2523. dev_info(dev, "%s: Micbias2 DT property not found\n",
  2524. __func__);
  2525. }
  2526. /* MB3 */
  2527. if (of_find_property(dev->of_node, "qcom,cdc-micbias3-mv",
  2528. NULL)) {
  2529. rc = wcd938x_read_of_property_u32(dev,
  2530. "qcom,cdc-micbias3-mv",
  2531. &prop_val);
  2532. if (!rc)
  2533. mb->micb3_mv = prop_val;
  2534. } else {
  2535. dev_info(dev, "%s: Micbias3 DT property not found\n",
  2536. __func__);
  2537. }
  2538. }
  2539. static int wcd938x_reset_low(struct device *dev)
  2540. {
  2541. struct wcd938x_priv *wcd938x = NULL;
  2542. int rc = 0;
  2543. if (!dev)
  2544. return -ENODEV;
  2545. wcd938x = dev_get_drvdata(dev);
  2546. if (!wcd938x)
  2547. return -EINVAL;
  2548. if (!wcd938x->rst_np) {
  2549. dev_err(dev, "%s: reset gpio device node not specified\n",
  2550. __func__);
  2551. return -EINVAL;
  2552. }
  2553. rc = msm_cdc_pinctrl_select_sleep_state(wcd938x->rst_np);
  2554. if (rc) {
  2555. dev_err(dev, "%s: wcd sleep state request fail!\n",
  2556. __func__);
  2557. return rc;
  2558. }
  2559. /* 20us sleep required after pulling the reset gpio to LOW */
  2560. usleep_range(20, 30);
  2561. return rc;
  2562. }
  2563. struct wcd938x_pdata *wcd938x_populate_dt_data(struct device *dev)
  2564. {
  2565. struct wcd938x_pdata *pdata = NULL;
  2566. pdata = devm_kzalloc(dev, sizeof(struct wcd938x_pdata),
  2567. GFP_KERNEL);
  2568. if (!pdata)
  2569. return NULL;
  2570. pdata->rst_np = of_parse_phandle(dev->of_node,
  2571. "qcom,wcd-rst-gpio-node", 0);
  2572. if (!pdata->rst_np) {
  2573. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  2574. __func__, "qcom,wcd-rst-gpio-node",
  2575. dev->of_node->full_name);
  2576. return NULL;
  2577. }
  2578. /* Parse power supplies */
  2579. msm_cdc_get_power_supplies(dev, &pdata->regulator,
  2580. &pdata->num_supplies);
  2581. if (!pdata->regulator || (pdata->num_supplies <= 0)) {
  2582. dev_err(dev, "%s: no power supplies defined for codec\n",
  2583. __func__);
  2584. return NULL;
  2585. }
  2586. pdata->rx_slave = of_parse_phandle(dev->of_node, "qcom,rx-slave", 0);
  2587. pdata->tx_slave = of_parse_phandle(dev->of_node, "qcom,tx-slave", 0);
  2588. wcd938x_dt_parse_micbias_info(dev, &pdata->micbias);
  2589. return pdata;
  2590. }
  2591. static irqreturn_t wcd938x_wd_handle_irq(int irq, void *data)
  2592. {
  2593. pr_err_ratelimited("%s: Watchdog interrupt for irq =%d triggered\n",
  2594. __func__, irq);
  2595. return IRQ_HANDLED;
  2596. }
  2597. static int wcd938x_bind(struct device *dev)
  2598. {
  2599. int ret = 0, i = 0;
  2600. struct wcd938x_pdata *pdata = dev_get_platdata(dev);
  2601. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  2602. /*
  2603. * Add 5msec delay to provide sufficient time for
  2604. * soundwire auto enumeration of slave devices as
  2605. * as per HW requirement.
  2606. */
  2607. usleep_range(5000, 5010);
  2608. ret = component_bind_all(dev, wcd938x);
  2609. if (ret) {
  2610. dev_err(dev, "%s: Slave bind failed, ret = %d\n",
  2611. __func__, ret);
  2612. return ret;
  2613. }
  2614. wcd938x->rx_swr_dev = get_matching_swr_slave_device(pdata->rx_slave);
  2615. if (!wcd938x->rx_swr_dev) {
  2616. dev_err(dev, "%s: Could not find RX swr slave device\n",
  2617. __func__);
  2618. ret = -ENODEV;
  2619. goto err;
  2620. }
  2621. wcd938x->tx_swr_dev = get_matching_swr_slave_device(pdata->tx_slave);
  2622. if (!wcd938x->tx_swr_dev) {
  2623. dev_err(dev, "%s: Could not find TX swr slave device\n",
  2624. __func__);
  2625. ret = -ENODEV;
  2626. goto err;
  2627. }
  2628. wcd938x->regmap = devm_regmap_init_swr(wcd938x->tx_swr_dev,
  2629. &wcd938x_regmap_config);
  2630. if (!wcd938x->regmap) {
  2631. dev_err(dev, "%s: Regmap init failed\n",
  2632. __func__);
  2633. goto err;
  2634. }
  2635. /* Set all interupts as edge triggered */
  2636. for (i = 0; i < wcd938x_regmap_irq_chip.num_regs; i++)
  2637. regmap_write(wcd938x->regmap,
  2638. (WCD938X_DIGITAL_INTR_LEVEL_0 + i), 0);
  2639. wcd938x_regmap_irq_chip.irq_drv_data = wcd938x;
  2640. wcd938x->irq_info.wcd_regmap_irq_chip = &wcd938x_regmap_irq_chip;
  2641. wcd938x->irq_info.codec_name = "WCD938X";
  2642. wcd938x->irq_info.regmap = wcd938x->regmap;
  2643. wcd938x->irq_info.dev = dev;
  2644. ret = wcd_irq_init(&wcd938x->irq_info, &wcd938x->virq);
  2645. if (ret) {
  2646. dev_err(wcd938x->dev, "%s: IRQ init failed: %d\n",
  2647. __func__, ret);
  2648. goto err;
  2649. }
  2650. wcd938x->tx_swr_dev->slave_irq = wcd938x->virq;
  2651. /* Request for watchdog interrupt */
  2652. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT,
  2653. "HPHR PDM WD INT", wcd938x_wd_handle_irq, NULL);
  2654. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT,
  2655. "HPHL PDM WD INT", wcd938x_wd_handle_irq, NULL);
  2656. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT,
  2657. "AUX PDM WD INT", wcd938x_wd_handle_irq, NULL);
  2658. /* Disable watchdog interrupt for HPH and AUX */
  2659. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT);
  2660. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT);
  2661. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT);
  2662. ret = snd_soc_register_component(dev, &soc_codec_dev_wcd938x,
  2663. NULL, 0);
  2664. if (ret) {
  2665. dev_err(dev, "%s: Codec registration failed\n",
  2666. __func__);
  2667. goto err_irq;
  2668. }
  2669. return ret;
  2670. err_irq:
  2671. wcd_irq_exit(&wcd938x->irq_info, wcd938x->virq);
  2672. err:
  2673. component_unbind_all(dev, wcd938x);
  2674. return ret;
  2675. }
  2676. static void wcd938x_unbind(struct device *dev)
  2677. {
  2678. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  2679. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT, NULL);
  2680. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT, NULL);
  2681. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT, NULL);
  2682. wcd_irq_exit(&wcd938x->irq_info, wcd938x->virq);
  2683. snd_soc_unregister_component(dev);
  2684. component_unbind_all(dev, wcd938x);
  2685. }
  2686. static const struct of_device_id wcd938x_dt_match[] = {
  2687. { .compatible = "qcom,wcd938x-codec" },
  2688. {}
  2689. };
  2690. static const struct component_master_ops wcd938x_comp_ops = {
  2691. .bind = wcd938x_bind,
  2692. .unbind = wcd938x_unbind,
  2693. };
  2694. static int wcd938x_compare_of(struct device *dev, void *data)
  2695. {
  2696. return dev->of_node == data;
  2697. }
  2698. static void wcd938x_release_of(struct device *dev, void *data)
  2699. {
  2700. of_node_put(data);
  2701. }
  2702. static int wcd938x_add_slave_components(struct device *dev,
  2703. struct component_match **matchptr)
  2704. {
  2705. struct device_node *np, *rx_node, *tx_node;
  2706. np = dev->of_node;
  2707. rx_node = of_parse_phandle(np, "qcom,rx-slave", 0);
  2708. if (!rx_node) {
  2709. dev_err(dev, "%s: Rx-slave node not defined\n", __func__);
  2710. return -ENODEV;
  2711. }
  2712. of_node_get(rx_node);
  2713. component_match_add_release(dev, matchptr,
  2714. wcd938x_release_of,
  2715. wcd938x_compare_of,
  2716. rx_node);
  2717. tx_node = of_parse_phandle(np, "qcom,tx-slave", 0);
  2718. if (!tx_node) {
  2719. dev_err(dev, "%s: Tx-slave node not defined\n", __func__);
  2720. return -ENODEV;
  2721. }
  2722. of_node_get(tx_node);
  2723. component_match_add_release(dev, matchptr,
  2724. wcd938x_release_of,
  2725. wcd938x_compare_of,
  2726. tx_node);
  2727. return 0;
  2728. }
  2729. static int wcd938x_wakeup(void *handle, bool enable)
  2730. {
  2731. struct wcd938x_priv *priv;
  2732. if (!handle) {
  2733. pr_err("%s: NULL handle\n", __func__);
  2734. return -EINVAL;
  2735. }
  2736. priv = (struct wcd938x_priv *)handle;
  2737. if (!priv->tx_swr_dev) {
  2738. pr_err("%s: tx swr dev is NULL\n", __func__);
  2739. return -EINVAL;
  2740. }
  2741. if (enable)
  2742. return swr_device_wakeup_vote(priv->tx_swr_dev);
  2743. else
  2744. return swr_device_wakeup_unvote(priv->tx_swr_dev);
  2745. }
  2746. static int wcd938x_probe(struct platform_device *pdev)
  2747. {
  2748. struct component_match *match = NULL;
  2749. struct wcd938x_priv *wcd938x = NULL;
  2750. struct wcd938x_pdata *pdata = NULL;
  2751. struct wcd_ctrl_platform_data *plat_data = NULL;
  2752. struct device *dev = &pdev->dev;
  2753. int ret;
  2754. wcd938x = devm_kzalloc(dev, sizeof(struct wcd938x_priv),
  2755. GFP_KERNEL);
  2756. if (!wcd938x)
  2757. return -ENOMEM;
  2758. dev_set_drvdata(dev, wcd938x);
  2759. wcd938x->dev = dev;
  2760. pdata = wcd938x_populate_dt_data(dev);
  2761. if (!pdata) {
  2762. dev_err(dev, "%s: Fail to obtain platform data\n", __func__);
  2763. return -EINVAL;
  2764. }
  2765. dev->platform_data = pdata;
  2766. wcd938x->rst_np = pdata->rst_np;
  2767. ret = msm_cdc_init_supplies(dev, &wcd938x->supplies,
  2768. pdata->regulator, pdata->num_supplies);
  2769. if (!wcd938x->supplies) {
  2770. dev_err(dev, "%s: Cannot init wcd supplies\n",
  2771. __func__);
  2772. return ret;
  2773. }
  2774. plat_data = dev_get_platdata(dev->parent);
  2775. if (!plat_data) {
  2776. dev_err(dev, "%s: platform data from parent is NULL\n",
  2777. __func__);
  2778. return -EINVAL;
  2779. }
  2780. wcd938x->handle = (void *)plat_data->handle;
  2781. if (!wcd938x->handle) {
  2782. dev_err(dev, "%s: handle is NULL\n", __func__);
  2783. return -EINVAL;
  2784. }
  2785. wcd938x->update_wcd_event = plat_data->update_wcd_event;
  2786. if (!wcd938x->update_wcd_event) {
  2787. dev_err(dev, "%s: update_wcd_event api is null!\n",
  2788. __func__);
  2789. return -EINVAL;
  2790. }
  2791. wcd938x->register_notifier = plat_data->register_notifier;
  2792. if (!wcd938x->register_notifier) {
  2793. dev_err(dev, "%s: register_notifier api is null!\n",
  2794. __func__);
  2795. return -EINVAL;
  2796. }
  2797. ret = msm_cdc_enable_static_supplies(&pdev->dev, wcd938x->supplies,
  2798. pdata->regulator,
  2799. pdata->num_supplies);
  2800. if (ret) {
  2801. dev_err(dev, "%s: wcd static supply enable failed!\n",
  2802. __func__);
  2803. return ret;
  2804. }
  2805. ret = wcd938x_parse_port_mapping(dev, "qcom,rx_swr_ch_map",
  2806. CODEC_RX);
  2807. ret |= wcd938x_parse_port_mapping(dev, "qcom,tx_swr_ch_map",
  2808. CODEC_TX);
  2809. if (ret) {
  2810. dev_err(dev, "Failed to read port mapping\n");
  2811. goto err;
  2812. }
  2813. mutex_init(&wcd938x->micb_lock);
  2814. ret = wcd938x_add_slave_components(dev, &match);
  2815. if (ret)
  2816. goto err_lock_init;
  2817. wcd938x_reset(dev);
  2818. wcd938x->wakeup = wcd938x_wakeup;
  2819. return component_master_add_with_match(dev,
  2820. &wcd938x_comp_ops, match);
  2821. err_lock_init:
  2822. mutex_destroy(&wcd938x->micb_lock);
  2823. err:
  2824. return ret;
  2825. }
  2826. static int wcd938x_remove(struct platform_device *pdev)
  2827. {
  2828. struct wcd938x_priv *wcd938x = NULL;
  2829. wcd938x = platform_get_drvdata(pdev);
  2830. component_master_del(&pdev->dev, &wcd938x_comp_ops);
  2831. mutex_destroy(&wcd938x->micb_lock);
  2832. dev_set_drvdata(&pdev->dev, NULL);
  2833. return 0;
  2834. }
  2835. #ifdef CONFIG_PM_SLEEP
  2836. static int wcd938x_suspend(struct device *dev)
  2837. {
  2838. return 0;
  2839. }
  2840. static int wcd938x_resume(struct device *dev)
  2841. {
  2842. return 0;
  2843. }
  2844. static const struct dev_pm_ops wcd938x_dev_pm_ops = {
  2845. SET_SYSTEM_SLEEP_PM_OPS(
  2846. wcd938x_suspend,
  2847. wcd938x_resume
  2848. )
  2849. };
  2850. #endif
  2851. static struct platform_driver wcd938x_codec_driver = {
  2852. .probe = wcd938x_probe,
  2853. .remove = wcd938x_remove,
  2854. .driver = {
  2855. .name = "wcd938x_codec",
  2856. .owner = THIS_MODULE,
  2857. .of_match_table = of_match_ptr(wcd938x_dt_match),
  2858. #ifdef CONFIG_PM_SLEEP
  2859. .pm = &wcd938x_dev_pm_ops,
  2860. #endif
  2861. .suppress_bind_attrs = true,
  2862. },
  2863. };
  2864. module_platform_driver(wcd938x_codec_driver);
  2865. MODULE_DESCRIPTION("WCD938X Codec driver");
  2866. MODULE_LICENSE("GPL v2");