wcd937x.c 70 KB

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