wcd937x.c 61 KB

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