wcd937x.c 59 KB

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