wcd937x.c 87 KB

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