wcd937x.c 70 KB

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