wcd-mbhc-v2.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/slab.h>
  7. #include <linux/of_gpio.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/device.h>
  10. #include <linux/printk.h>
  11. #include <linux/ratelimit.h>
  12. #include <linux/list.h>
  13. #include <linux/bitops.h>
  14. #include <linux/delay.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/kernel.h>
  17. #include <linux/input.h>
  18. #include <linux/firmware.h>
  19. #include <linux/completion.h>
  20. #include <linux/soc/qcom/fsa4480-i2c.h>
  21. #include <sound/soc.h>
  22. #include <sound/jack.h>
  23. #include "msm-cdc-pinctrl.h"
  24. #include "wcdcal-hwdep.h"
  25. #include "wcd-mbhc-legacy.h"
  26. #include "wcd-mbhc-adc.h"
  27. #include "wcd-mbhc-v2-api.h"
  28. void wcd_mbhc_jack_report(struct wcd_mbhc *mbhc,
  29. struct snd_soc_jack *jack, int status, int mask)
  30. {
  31. snd_soc_jack_report(jack, status, mask);
  32. }
  33. EXPORT_SYMBOL(wcd_mbhc_jack_report);
  34. static void __hphocp_off_report(struct wcd_mbhc *mbhc, u32 jack_status,
  35. int irq)
  36. {
  37. struct snd_soc_codec *codec = mbhc->codec;
  38. dev_dbg(codec->dev, "%s: clear ocp status %x\n",
  39. __func__, jack_status);
  40. if (mbhc->hph_status & jack_status) {
  41. mbhc->hph_status &= ~jack_status;
  42. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  43. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  44. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  45. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  46. /*
  47. * reset retry counter as PA is turned off signifying
  48. * start of new OCP detection session
  49. */
  50. if (mbhc->intr_ids->hph_left_ocp)
  51. mbhc->hphlocp_cnt = 0;
  52. else
  53. mbhc->hphrocp_cnt = 0;
  54. mbhc->mbhc_cb->irq_control(codec, irq, true);
  55. }
  56. }
  57. static void hphrocp_off_report(struct wcd_mbhc *mbhc, u32 jack_status)
  58. {
  59. __hphocp_off_report(mbhc, SND_JACK_OC_HPHR,
  60. mbhc->intr_ids->hph_right_ocp);
  61. }
  62. static void hphlocp_off_report(struct wcd_mbhc *mbhc, u32 jack_status)
  63. {
  64. __hphocp_off_report(mbhc, SND_JACK_OC_HPHL,
  65. mbhc->intr_ids->hph_left_ocp);
  66. }
  67. static void wcd_program_hs_vref(struct wcd_mbhc *mbhc)
  68. {
  69. struct wcd_mbhc_plug_type_cfg *plug_type_cfg;
  70. struct snd_soc_codec *codec = mbhc->codec;
  71. u32 reg_val;
  72. plug_type_cfg = WCD_MBHC_CAL_PLUG_TYPE_PTR(mbhc->mbhc_cfg->calibration);
  73. reg_val = ((plug_type_cfg->v_hs_max - HS_VREF_MIN_VAL) / 100);
  74. dev_dbg(codec->dev, "%s: reg_val = %x\n", __func__, reg_val);
  75. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_VREF, reg_val);
  76. }
  77. static void wcd_program_btn_threshold(const struct wcd_mbhc *mbhc, bool micbias)
  78. {
  79. struct wcd_mbhc_btn_detect_cfg *btn_det;
  80. struct snd_soc_codec *codec = mbhc->codec;
  81. struct snd_soc_card *card = codec->component.card;
  82. s16 *btn_low, *btn_high;
  83. if (mbhc->mbhc_cfg->calibration == NULL) {
  84. dev_err(card->dev, "%s: calibration data is NULL\n", __func__);
  85. return;
  86. }
  87. btn_det = WCD_MBHC_CAL_BTN_DET_PTR(mbhc->mbhc_cfg->calibration);
  88. btn_low = btn_det->_v_btn_low;
  89. btn_high = ((void *)&btn_det->_v_btn_low) +
  90. (sizeof(btn_det->_v_btn_low[0]) * btn_det->num_btn);
  91. mbhc->mbhc_cb->set_btn_thr(codec, btn_low, btn_high, btn_det->num_btn,
  92. micbias);
  93. }
  94. void wcd_enable_curr_micbias(const struct wcd_mbhc *mbhc,
  95. const enum wcd_mbhc_cs_mb_en_flag cs_mb_en)
  96. {
  97. /*
  98. * Some codecs handle micbias/pullup enablement in codec
  99. * drivers itself and micbias is not needed for regular
  100. * plug type detection. So if micbias_control callback function
  101. * is defined, just return.
  102. */
  103. if (mbhc->mbhc_cb->mbhc_micbias_control)
  104. return;
  105. pr_debug("%s: enter, cs_mb_en: %d\n", __func__, cs_mb_en);
  106. switch (cs_mb_en) {
  107. case WCD_MBHC_EN_CS:
  108. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  109. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 3);
  110. /* Program Button threshold registers as per CS */
  111. wcd_program_btn_threshold(mbhc, false);
  112. break;
  113. case WCD_MBHC_EN_MB:
  114. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  115. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  116. /* Disable PULL_UP_EN & enable MICBIAS */
  117. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 2);
  118. /* Program Button threshold registers as per MICBIAS */
  119. wcd_program_btn_threshold(mbhc, true);
  120. break;
  121. case WCD_MBHC_EN_PULLUP:
  122. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 3);
  123. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  124. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 1);
  125. /* Program Button threshold registers as per MICBIAS */
  126. wcd_program_btn_threshold(mbhc, true);
  127. break;
  128. case WCD_MBHC_EN_NONE:
  129. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  130. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  131. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  132. break;
  133. default:
  134. pr_debug("%s: Invalid parameter", __func__);
  135. break;
  136. }
  137. pr_debug("%s: exit\n", __func__);
  138. }
  139. EXPORT_SYMBOL(wcd_enable_curr_micbias);
  140. static const char *wcd_mbhc_get_event_string(int event)
  141. {
  142. switch (event) {
  143. case WCD_EVENT_PRE_MICBIAS_2_OFF:
  144. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_MICBIAS_2_OFF);
  145. case WCD_EVENT_POST_MICBIAS_2_OFF:
  146. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_MICBIAS_2_OFF);
  147. case WCD_EVENT_PRE_MICBIAS_2_ON:
  148. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_MICBIAS_2_ON);
  149. case WCD_EVENT_POST_MICBIAS_2_ON:
  150. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_MICBIAS_2_ON);
  151. case WCD_EVENT_PRE_HPHL_PA_ON:
  152. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_HPHL_PA_ON);
  153. case WCD_EVENT_POST_HPHL_PA_OFF:
  154. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_HPHL_PA_OFF);
  155. case WCD_EVENT_PRE_HPHR_PA_ON:
  156. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_HPHR_PA_ON);
  157. case WCD_EVENT_POST_HPHR_PA_OFF:
  158. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_HPHR_PA_OFF);
  159. case WCD_EVENT_PRE_HPHR_PA_OFF:
  160. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_HPHR_PA_OFF);
  161. case WCD_EVENT_PRE_HPHL_PA_OFF:
  162. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_HPHL_PA_OFF);
  163. case WCD_EVENT_POST_DAPM_MICBIAS_2_ON:
  164. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_DAPM_MICBIAS_2_ON);
  165. case WCD_EVENT_PRE_DAPM_MICBIAS_2_ON:
  166. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_DAPM_MICBIAS_2_ON);
  167. case WCD_EVENT_POST_DAPM_MICBIAS_2_OFF:
  168. return WCD_MBHC_STRINGIFY(WCD_EVENT_POST_DAPM_MICBIAS_2_OFF);
  169. case WCD_EVENT_PRE_DAPM_MICBIAS_2_OFF:
  170. return WCD_MBHC_STRINGIFY(WCD_EVENT_PRE_DAPM_MICBIAS_2_OFF);
  171. case WCD_EVENT_OCP_OFF:
  172. return WCD_MBHC_STRINGIFY(WCD_EVENT_OCP_OFF);
  173. case WCD_EVENT_OCP_ON:
  174. return WCD_MBHC_STRINGIFY(WCD_EVENT_OCP_ON);
  175. case WCD_EVENT_INVALID:
  176. default:
  177. return WCD_MBHC_STRINGIFY(WCD_EVENT_INVALID);
  178. }
  179. }
  180. static int wcd_event_notify(struct notifier_block *self, unsigned long val,
  181. void *data)
  182. {
  183. struct wcd_mbhc *mbhc = (struct wcd_mbhc *)data;
  184. enum wcd_notify_event event = (enum wcd_notify_event)val;
  185. struct snd_soc_codec *codec = mbhc->codec;
  186. bool micbias2 = false;
  187. bool micbias1 = false;
  188. u8 fsm_en = 0;
  189. pr_debug("%s: event %s (%d)\n", __func__,
  190. wcd_mbhc_get_event_string(event), event);
  191. if (mbhc->mbhc_cb->micbias_enable_status) {
  192. micbias2 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  193. MIC_BIAS_2);
  194. micbias1 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  195. MIC_BIAS_1);
  196. }
  197. switch (event) {
  198. /* MICBIAS usage change */
  199. case WCD_EVENT_POST_DAPM_MICBIAS_2_ON:
  200. mbhc->is_hs_recording = true;
  201. pr_debug("%s: is_capture: %d\n", __func__,
  202. mbhc->is_hs_recording);
  203. break;
  204. case WCD_EVENT_POST_MICBIAS_2_ON:
  205. if (!mbhc->micbias_enable)
  206. goto out_micb_en;
  207. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl) {
  208. mbhc->mbhc_cb->mbhc_common_micb_ctrl(codec,
  209. MBHC_COMMON_MICB_PRECHARGE,
  210. true);
  211. mbhc->mbhc_cb->mbhc_common_micb_ctrl(codec,
  212. MBHC_COMMON_MICB_SET_VAL,
  213. true);
  214. /*
  215. * Special headset needs MICBIAS as 2.7V so wait for
  216. * 50 msec for the MICBIAS to reach 2.7 volts.
  217. */
  218. msleep(50);
  219. }
  220. if (mbhc->mbhc_cb->set_auto_zeroing)
  221. mbhc->mbhc_cb->set_auto_zeroing(codec, true);
  222. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  223. mbhc->mbhc_cb->mbhc_common_micb_ctrl(codec,
  224. MBHC_COMMON_MICB_PRECHARGE,
  225. false);
  226. out_micb_en:
  227. /* Disable current source if micbias enabled */
  228. if (mbhc->mbhc_cb->mbhc_micbias_control) {
  229. WCD_MBHC_REG_READ(WCD_MBHC_FSM_EN, fsm_en);
  230. if (fsm_en)
  231. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL,
  232. 0);
  233. } else {
  234. mbhc->is_hs_recording = true;
  235. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  236. }
  237. /* configure cap settings properly when micbias is enabled */
  238. if (mbhc->mbhc_cb->set_cap_mode)
  239. mbhc->mbhc_cb->set_cap_mode(codec, micbias1, true);
  240. break;
  241. case WCD_EVENT_PRE_MICBIAS_2_OFF:
  242. /*
  243. * Before MICBIAS_2 is turned off, if FSM is enabled,
  244. * make sure current source is enabled so as to detect
  245. * button press/release events
  246. */
  247. if (mbhc->mbhc_cb->mbhc_micbias_control &&
  248. !mbhc->micbias_enable) {
  249. WCD_MBHC_REG_READ(WCD_MBHC_FSM_EN, fsm_en);
  250. if (fsm_en)
  251. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL,
  252. 3);
  253. }
  254. break;
  255. /* MICBIAS usage change */
  256. case WCD_EVENT_POST_DAPM_MICBIAS_2_OFF:
  257. mbhc->is_hs_recording = false;
  258. pr_debug("%s: is_capture: %d\n", __func__,
  259. mbhc->is_hs_recording);
  260. break;
  261. case WCD_EVENT_POST_MICBIAS_2_OFF:
  262. if (!mbhc->mbhc_cb->mbhc_micbias_control)
  263. mbhc->is_hs_recording = false;
  264. if (mbhc->micbias_enable) {
  265. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  266. break;
  267. }
  268. if (mbhc->mbhc_cb->set_auto_zeroing)
  269. mbhc->mbhc_cb->set_auto_zeroing(codec, false);
  270. if (mbhc->mbhc_cb->set_micbias_value && !mbhc->micbias_enable)
  271. mbhc->mbhc_cb->set_micbias_value(codec);
  272. /* Enable PULL UP if PA's are enabled */
  273. if ((test_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state)) ||
  274. (test_bit(WCD_MBHC_EVENT_PA_HPHR,
  275. &mbhc->event_state)))
  276. /* enable pullup and cs, disable mb */
  277. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_PULLUP);
  278. else
  279. /* enable current source and disable mb, pullup*/
  280. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_CS);
  281. /* configure cap settings properly when micbias is disabled */
  282. if (mbhc->mbhc_cb->set_cap_mode)
  283. mbhc->mbhc_cb->set_cap_mode(codec, micbias1, false);
  284. break;
  285. case WCD_EVENT_PRE_HPHL_PA_OFF:
  286. mutex_lock(&mbhc->hphl_pa_lock);
  287. break;
  288. case WCD_EVENT_POST_HPHL_PA_OFF:
  289. clear_bit(WCD_MBHC_HPHL_PA_OFF_ACK, &mbhc->hph_pa_dac_state);
  290. if (mbhc->hph_status & SND_JACK_OC_HPHL)
  291. hphlocp_off_report(mbhc, SND_JACK_OC_HPHL);
  292. clear_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state);
  293. /* check if micbias is enabled */
  294. if (micbias2)
  295. /* Disable cs, pullup & enable micbias */
  296. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  297. else
  298. /* Disable micbias, pullup & enable cs */
  299. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_CS);
  300. mutex_unlock(&mbhc->hphl_pa_lock);
  301. clear_bit(WCD_MBHC_ANC0_OFF_ACK, &mbhc->hph_anc_state);
  302. break;
  303. case WCD_EVENT_PRE_HPHR_PA_OFF:
  304. mutex_lock(&mbhc->hphr_pa_lock);
  305. break;
  306. case WCD_EVENT_POST_HPHR_PA_OFF:
  307. clear_bit(WCD_MBHC_HPHR_PA_OFF_ACK, &mbhc->hph_pa_dac_state);
  308. if (mbhc->hph_status & SND_JACK_OC_HPHR)
  309. hphrocp_off_report(mbhc, SND_JACK_OC_HPHR);
  310. clear_bit(WCD_MBHC_EVENT_PA_HPHR, &mbhc->event_state);
  311. /* check if micbias is enabled */
  312. if (micbias2)
  313. /* Disable cs, pullup & enable micbias */
  314. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  315. else
  316. /* Disable micbias, pullup & enable cs */
  317. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_CS);
  318. mutex_unlock(&mbhc->hphr_pa_lock);
  319. clear_bit(WCD_MBHC_ANC1_OFF_ACK, &mbhc->hph_anc_state);
  320. break;
  321. case WCD_EVENT_PRE_HPHL_PA_ON:
  322. set_bit(WCD_MBHC_EVENT_PA_HPHL, &mbhc->event_state);
  323. /* check if micbias is enabled */
  324. if (micbias2)
  325. /* Disable cs, pullup & enable micbias */
  326. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  327. else
  328. /* Disable micbias, enable pullup & cs */
  329. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_PULLUP);
  330. break;
  331. case WCD_EVENT_PRE_HPHR_PA_ON:
  332. set_bit(WCD_MBHC_EVENT_PA_HPHR, &mbhc->event_state);
  333. /* check if micbias is enabled */
  334. if (micbias2)
  335. /* Disable cs, pullup & enable micbias */
  336. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  337. else
  338. /* Disable micbias, enable pullup & cs */
  339. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_PULLUP);
  340. break;
  341. case WCD_EVENT_OCP_OFF:
  342. mbhc->mbhc_cb->irq_control(mbhc->codec,
  343. mbhc->intr_ids->hph_left_ocp,
  344. false);
  345. break;
  346. case WCD_EVENT_OCP_ON:
  347. mbhc->mbhc_cb->irq_control(mbhc->codec,
  348. mbhc->intr_ids->hph_left_ocp,
  349. true);
  350. break;
  351. default:
  352. break;
  353. }
  354. return 0;
  355. }
  356. int wcd_cancel_btn_work(struct wcd_mbhc *mbhc)
  357. {
  358. int r;
  359. r = cancel_delayed_work_sync(&mbhc->mbhc_btn_dwork);
  360. /*
  361. * if scheduled mbhc.mbhc_btn_dwork is canceled from here,
  362. * we have to unlock from here instead btn_work
  363. */
  364. if (r)
  365. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  366. return r;
  367. }
  368. EXPORT_SYMBOL(wcd_cancel_btn_work);
  369. bool wcd_swch_level_remove(struct wcd_mbhc *mbhc)
  370. {
  371. u16 result2 = 0;
  372. WCD_MBHC_REG_READ(WCD_MBHC_SWCH_LEVEL_REMOVE, result2);
  373. return (result2) ? true : false;
  374. }
  375. EXPORT_SYMBOL(wcd_swch_level_remove);
  376. static void wcd_mbhc_clr_and_turnon_hph_padac(struct wcd_mbhc *mbhc)
  377. {
  378. bool pa_turned_on = false;
  379. u8 wg_time = 0;
  380. WCD_MBHC_REG_READ(WCD_MBHC_HPH_CNP_WG_TIME, wg_time);
  381. wg_time += 1;
  382. mutex_lock(&mbhc->hphr_pa_lock);
  383. if (test_and_clear_bit(WCD_MBHC_HPHR_PA_OFF_ACK,
  384. &mbhc->hph_pa_dac_state)) {
  385. pr_debug("%s: HPHR clear flag and enable PA\n", __func__);
  386. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_PA_EN, 1);
  387. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_OCP_DET_EN, 1);
  388. pa_turned_on = true;
  389. }
  390. mutex_unlock(&mbhc->hphr_pa_lock);
  391. mutex_lock(&mbhc->hphl_pa_lock);
  392. if (test_and_clear_bit(WCD_MBHC_HPHL_PA_OFF_ACK,
  393. &mbhc->hph_pa_dac_state)) {
  394. pr_debug("%s: HPHL clear flag and enable PA\n", __func__);
  395. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_PA_EN, 1);
  396. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_OCP_DET_EN, 1);
  397. pa_turned_on = true;
  398. }
  399. mutex_unlock(&mbhc->hphl_pa_lock);
  400. if (pa_turned_on) {
  401. pr_debug("%s: PA was turned on by MBHC and not by DAPM\n",
  402. __func__);
  403. usleep_range(wg_time * 1000, wg_time * 1000 + 50);
  404. }
  405. if (test_and_clear_bit(WCD_MBHC_ANC0_OFF_ACK,
  406. &mbhc->hph_anc_state)) {
  407. usleep_range(20000, 20100);
  408. pr_debug("%s: HPHL ANC clear flag and enable ANC_EN\n",
  409. __func__);
  410. if (mbhc->mbhc_cb->update_anc_state)
  411. mbhc->mbhc_cb->update_anc_state(mbhc->codec, true, 0);
  412. }
  413. if (test_and_clear_bit(WCD_MBHC_ANC1_OFF_ACK,
  414. &mbhc->hph_anc_state)) {
  415. usleep_range(20000, 20100);
  416. pr_debug("%s: HPHR ANC clear flag and enable ANC_EN\n",
  417. __func__);
  418. if (mbhc->mbhc_cb->update_anc_state)
  419. mbhc->mbhc_cb->update_anc_state(mbhc->codec, true, 1);
  420. }
  421. }
  422. static bool wcd_mbhc_is_hph_pa_on(struct wcd_mbhc *mbhc)
  423. {
  424. bool hph_pa_on = false;
  425. WCD_MBHC_REG_READ(WCD_MBHC_HPH_PA_EN, hph_pa_on);
  426. return (hph_pa_on) ? true : false;
  427. }
  428. static void wcd_mbhc_set_and_turnoff_hph_padac(struct wcd_mbhc *mbhc)
  429. {
  430. u8 wg_time = 0;
  431. WCD_MBHC_REG_READ(WCD_MBHC_HPH_CNP_WG_TIME, wg_time);
  432. wg_time += 1;
  433. /* If headphone PA is on, check if userspace receives
  434. * removal event to sync-up PA's state
  435. */
  436. if (wcd_mbhc_is_hph_pa_on(mbhc)) {
  437. pr_debug("%s PA is on, setting PA_OFF_ACK\n", __func__);
  438. set_bit(WCD_MBHC_HPHL_PA_OFF_ACK, &mbhc->hph_pa_dac_state);
  439. set_bit(WCD_MBHC_HPHR_PA_OFF_ACK, &mbhc->hph_pa_dac_state);
  440. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_OCP_DET_EN, 0);
  441. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHR_OCP_DET_EN, 0);
  442. } else {
  443. pr_debug("%s PA is off\n", __func__);
  444. }
  445. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPH_PA_EN, 0);
  446. usleep_range(wg_time * 1000, wg_time * 1000 + 50);
  447. if (mbhc->mbhc_cb->is_anc_on && mbhc->mbhc_cb->is_anc_on(mbhc)) {
  448. usleep_range(20000, 20100);
  449. pr_debug("%s ANC is on, setting ANC_OFF_ACK\n", __func__);
  450. set_bit(WCD_MBHC_ANC0_OFF_ACK, &mbhc->hph_anc_state);
  451. set_bit(WCD_MBHC_ANC1_OFF_ACK, &mbhc->hph_anc_state);
  452. if (mbhc->mbhc_cb->update_anc_state) {
  453. mbhc->mbhc_cb->update_anc_state(mbhc->codec, false, 0);
  454. mbhc->mbhc_cb->update_anc_state(mbhc->codec, false, 1);
  455. } else {
  456. pr_debug("%s ANC is off\n", __func__);
  457. }
  458. }
  459. }
  460. int wcd_mbhc_get_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
  461. uint32_t *zr)
  462. {
  463. *zl = mbhc->zl;
  464. *zr = mbhc->zr;
  465. if (*zl && *zr)
  466. return 0;
  467. else
  468. return -EINVAL;
  469. }
  470. EXPORT_SYMBOL(wcd_mbhc_get_impedance);
  471. void wcd_mbhc_hs_elec_irq(struct wcd_mbhc *mbhc, int irq_type,
  472. bool enable)
  473. {
  474. int irq;
  475. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  476. if (irq_type == WCD_MBHC_ELEC_HS_INS)
  477. irq = mbhc->intr_ids->mbhc_hs_ins_intr;
  478. else if (irq_type == WCD_MBHC_ELEC_HS_REM)
  479. irq = mbhc->intr_ids->mbhc_hs_rem_intr;
  480. else {
  481. pr_debug("%s: irq_type: %d, enable: %d\n",
  482. __func__, irq_type, enable);
  483. return;
  484. }
  485. pr_debug("%s: irq: %d, enable: %d, intr_status:%lu\n",
  486. __func__, irq, enable, mbhc->intr_status);
  487. if ((test_bit(irq_type, &mbhc->intr_status)) != enable) {
  488. mbhc->mbhc_cb->irq_control(mbhc->codec, irq, enable);
  489. if (enable)
  490. set_bit(irq_type, &mbhc->intr_status);
  491. else
  492. clear_bit(irq_type, &mbhc->intr_status);
  493. }
  494. }
  495. EXPORT_SYMBOL(wcd_mbhc_hs_elec_irq);
  496. void wcd_mbhc_report_plug(struct wcd_mbhc *mbhc, int insertion,
  497. enum snd_jack_types jack_type)
  498. {
  499. struct snd_soc_codec *codec = mbhc->codec;
  500. bool is_pa_on = false;
  501. u8 fsm_en = 0;
  502. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  503. pr_debug("%s: enter insertion %d hph_status %x\n",
  504. __func__, insertion, mbhc->hph_status);
  505. if (!insertion) {
  506. /* Report removal */
  507. mbhc->hph_status &= ~jack_type;
  508. /*
  509. * cancel possibly scheduled btn work and
  510. * report release if we reported button press
  511. */
  512. if (wcd_cancel_btn_work(mbhc)) {
  513. pr_debug("%s: button press is canceled\n", __func__);
  514. } else if (mbhc->buttons_pressed) {
  515. pr_debug("%s: release of button press%d\n",
  516. __func__, jack_type);
  517. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack, 0,
  518. mbhc->buttons_pressed);
  519. mbhc->buttons_pressed &=
  520. ~WCD_MBHC_JACK_BUTTON_MASK;
  521. }
  522. if (mbhc->micbias_enable) {
  523. if (mbhc->mbhc_cb->mbhc_micbias_control)
  524. mbhc->mbhc_cb->mbhc_micbias_control(
  525. codec, MIC_BIAS_2,
  526. MICB_DISABLE);
  527. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  528. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  529. codec,
  530. MIC_BIAS_2, false);
  531. if (mbhc->mbhc_cb->set_micbias_value) {
  532. mbhc->mbhc_cb->set_micbias_value(codec);
  533. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  534. }
  535. mbhc->micbias_enable = false;
  536. }
  537. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  538. mbhc->zl = mbhc->zr = 0;
  539. pr_debug("%s: Reporting removal %d(%x)\n", __func__,
  540. jack_type, mbhc->hph_status);
  541. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  542. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  543. wcd_mbhc_set_and_turnoff_hph_padac(mbhc);
  544. hphrocp_off_report(mbhc, SND_JACK_OC_HPHR);
  545. hphlocp_off_report(mbhc, SND_JACK_OC_HPHL);
  546. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  547. mbhc->force_linein = false;
  548. } else {
  549. /*
  550. * Report removal of current jack type.
  551. * Headphone to headset shouldn't report headphone
  552. * removal.
  553. */
  554. if (mbhc->mbhc_cfg->detect_extn_cable &&
  555. (mbhc->current_plug == MBHC_PLUG_TYPE_HIGH_HPH ||
  556. jack_type == SND_JACK_LINEOUT) &&
  557. (mbhc->hph_status && mbhc->hph_status != jack_type)) {
  558. if (mbhc->micbias_enable &&
  559. mbhc->hph_status == SND_JACK_HEADSET) {
  560. if (mbhc->mbhc_cb->mbhc_micbias_control)
  561. mbhc->mbhc_cb->mbhc_micbias_control(
  562. codec, MIC_BIAS_2,
  563. MICB_DISABLE);
  564. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  565. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  566. codec,
  567. MIC_BIAS_2, false);
  568. if (mbhc->mbhc_cb->set_micbias_value) {
  569. mbhc->mbhc_cb->set_micbias_value(
  570. codec);
  571. WCD_MBHC_REG_UPDATE_BITS(
  572. WCD_MBHC_MICB_CTRL, 0);
  573. }
  574. mbhc->micbias_enable = false;
  575. }
  576. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  577. mbhc->zl = mbhc->zr = 0;
  578. pr_debug("%s: Reporting removal (%x)\n",
  579. __func__, mbhc->hph_status);
  580. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  581. 0, WCD_MBHC_JACK_MASK);
  582. if (mbhc->hph_status == SND_JACK_LINEOUT) {
  583. pr_debug("%s: Enable micbias\n", __func__);
  584. /* Disable current source and enable micbias */
  585. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  586. pr_debug("%s: set up elec removal detection\n",
  587. __func__);
  588. usleep_range(200, 210);
  589. wcd_mbhc_hs_elec_irq(mbhc,
  590. WCD_MBHC_ELEC_HS_REM,
  591. true);
  592. }
  593. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  594. SND_JACK_LINEOUT |
  595. SND_JACK_ANC_HEADPHONE |
  596. SND_JACK_UNSUPPORTED);
  597. mbhc->force_linein = false;
  598. }
  599. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET &&
  600. jack_type == SND_JACK_HEADPHONE)
  601. mbhc->hph_status &= ~SND_JACK_HEADSET;
  602. /* Report insertion */
  603. if (jack_type == SND_JACK_HEADPHONE)
  604. mbhc->current_plug = MBHC_PLUG_TYPE_HEADPHONE;
  605. else if (jack_type == SND_JACK_UNSUPPORTED)
  606. mbhc->current_plug = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  607. else if (jack_type == SND_JACK_HEADSET) {
  608. mbhc->current_plug = MBHC_PLUG_TYPE_HEADSET;
  609. mbhc->jiffies_atreport = jiffies;
  610. } else if (jack_type == SND_JACK_LINEOUT) {
  611. mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
  612. } else if (jack_type == SND_JACK_ANC_HEADPHONE)
  613. mbhc->current_plug = MBHC_PLUG_TYPE_ANC_HEADPHONE;
  614. if (mbhc->mbhc_cb->hph_pa_on_status)
  615. is_pa_on = mbhc->mbhc_cb->hph_pa_on_status(codec);
  616. if (mbhc->impedance_detect &&
  617. mbhc->mbhc_cb->compute_impedance &&
  618. (mbhc->mbhc_cfg->linein_th != 0) &&
  619. (!is_pa_on)) {
  620. /* Set MUX_CTL to AUTO for Z-det */
  621. WCD_MBHC_REG_READ(WCD_MBHC_FSM_EN, fsm_en);
  622. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  623. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MUX_CTL,
  624. MUX_CTL_AUTO);
  625. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 1);
  626. mbhc->mbhc_cb->compute_impedance(mbhc,
  627. &mbhc->zl, &mbhc->zr);
  628. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN,
  629. fsm_en);
  630. if ((mbhc->zl > mbhc->mbhc_cfg->linein_th &&
  631. mbhc->zl < MAX_IMPED) &&
  632. (mbhc->zr > mbhc->mbhc_cfg->linein_th &&
  633. mbhc->zr < MAX_IMPED) &&
  634. (jack_type == SND_JACK_HEADPHONE)) {
  635. jack_type = SND_JACK_LINEOUT;
  636. mbhc->force_linein = true;
  637. mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
  638. if (mbhc->hph_status) {
  639. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  640. SND_JACK_LINEOUT |
  641. SND_JACK_UNSUPPORTED);
  642. wcd_mbhc_jack_report(mbhc,
  643. &mbhc->headset_jack,
  644. mbhc->hph_status,
  645. WCD_MBHC_JACK_MASK);
  646. }
  647. pr_debug("%s: Marking jack type as SND_JACK_LINEOUT\n",
  648. __func__);
  649. }
  650. }
  651. mbhc->hph_status |= jack_type;
  652. pr_debug("%s: Reporting insertion %d(%x)\n", __func__,
  653. jack_type, mbhc->hph_status);
  654. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  655. (mbhc->hph_status | SND_JACK_MECHANICAL),
  656. WCD_MBHC_JACK_MASK);
  657. wcd_mbhc_clr_and_turnon_hph_padac(mbhc);
  658. }
  659. pr_debug("%s: leave hph_status %x\n", __func__, mbhc->hph_status);
  660. }
  661. EXPORT_SYMBOL(wcd_mbhc_report_plug);
  662. void wcd_mbhc_elec_hs_report_unplug(struct wcd_mbhc *mbhc)
  663. {
  664. /* cancel pending button press */
  665. if (wcd_cancel_btn_work(mbhc))
  666. pr_debug("%s: button press is canceled\n", __func__);
  667. /* cancel correct work function */
  668. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug)
  669. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  670. &mbhc->correct_plug_swch);
  671. else
  672. pr_info("%s: hs_detect_plug work not cancelled\n", __func__);
  673. pr_debug("%s: Report extension cable\n", __func__);
  674. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  675. /*
  676. * If PA is enabled HPHL schmitt trigger can
  677. * be unreliable, make sure to disable it
  678. */
  679. if (test_bit(WCD_MBHC_EVENT_PA_HPHL,
  680. &mbhc->event_state))
  681. wcd_mbhc_set_and_turnoff_hph_padac(mbhc);
  682. /*
  683. * Disable HPHL trigger and MIC Schmitt triggers.
  684. * Setup for insertion detection.
  685. */
  686. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM,
  687. false);
  688. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_NONE);
  689. /* Disable HW FSM */
  690. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  691. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 3);
  692. /* Set the detection type appropriately */
  693. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE, 1);
  694. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  695. true);
  696. }
  697. EXPORT_SYMBOL(wcd_mbhc_elec_hs_report_unplug);
  698. void wcd_mbhc_find_plug_and_report(struct wcd_mbhc *mbhc,
  699. enum wcd_mbhc_plug_type plug_type)
  700. {
  701. bool anc_mic_found = false;
  702. enum snd_jack_types jack_type;
  703. if (mbhc->deinit_in_progress) {
  704. pr_info("%s: mbhc deinit in progess: ignore report\n", __func__);
  705. return;
  706. }
  707. pr_debug("%s: enter current_plug(%d) new_plug(%d)\n",
  708. __func__, mbhc->current_plug, plug_type);
  709. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  710. if (mbhc->current_plug == plug_type) {
  711. pr_debug("%s: cable already reported, exit\n", __func__);
  712. goto exit;
  713. }
  714. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE) {
  715. /*
  716. * Nothing was reported previously
  717. * report a headphone or unsupported
  718. */
  719. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_HEADPHONE);
  720. } else if (plug_type == MBHC_PLUG_TYPE_GND_MIC_SWAP) {
  721. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  722. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADPHONE);
  723. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET)
  724. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADSET);
  725. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_UNSUPPORTED);
  726. } else if (plug_type == MBHC_PLUG_TYPE_HEADSET) {
  727. if (mbhc->mbhc_cfg->enable_anc_mic_detect &&
  728. mbhc->mbhc_fn->wcd_mbhc_detect_anc_plug_type)
  729. anc_mic_found =
  730. mbhc->mbhc_fn->wcd_mbhc_detect_anc_plug_type(mbhc);
  731. jack_type = SND_JACK_HEADSET;
  732. if (anc_mic_found)
  733. jack_type = SND_JACK_ANC_HEADPHONE;
  734. /*
  735. * If Headphone was reported previously, this will
  736. * only report the mic line
  737. */
  738. wcd_mbhc_report_plug(mbhc, 1, jack_type);
  739. } else if (plug_type == MBHC_PLUG_TYPE_HIGH_HPH) {
  740. if (mbhc->mbhc_cfg->detect_extn_cable) {
  741. /* High impedance device found. Report as LINEOUT */
  742. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  743. pr_debug("%s: setup mic trigger for further detection\n",
  744. __func__);
  745. /* Disable HW FSM and current source */
  746. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  747. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  748. /* Setup for insertion detection */
  749. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE,
  750. 1);
  751. /*
  752. * Enable HPHL trigger and MIC Schmitt triggers
  753. * and request for elec insertion interrupts
  754. */
  755. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC,
  756. 3);
  757. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  758. true);
  759. } else {
  760. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  761. }
  762. } else {
  763. WARN(1, "Unexpected current plug_type %d, plug_type %d\n",
  764. mbhc->current_plug, plug_type);
  765. }
  766. exit:
  767. pr_debug("%s: leave\n", __func__);
  768. }
  769. EXPORT_SYMBOL(wcd_mbhc_find_plug_and_report);
  770. static bool wcd_mbhc_moisture_detect(struct wcd_mbhc *mbhc, bool detection_type)
  771. {
  772. bool ret = false;
  773. if (!mbhc->mbhc_cfg->moisture_en ||
  774. !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  775. return ret;
  776. if (!mbhc->mbhc_cb->mbhc_get_moisture_status ||
  777. !mbhc->mbhc_cb->mbhc_moisture_polling_ctrl ||
  778. !mbhc->mbhc_cb->mbhc_moisture_detect_en)
  779. return ret;
  780. if (mbhc->mbhc_cb->mbhc_get_moisture_status(mbhc)) {
  781. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  782. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_DET_EN, 0);
  783. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc, true);
  784. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  785. detection_type);
  786. ret = true;
  787. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  788. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_DET_EN, 1);
  789. } else {
  790. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc, false);
  791. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc, false);
  792. }
  793. return ret;
  794. }
  795. static void wcd_mbhc_swch_irq_handler(struct wcd_mbhc *mbhc)
  796. {
  797. bool detection_type = 0;
  798. bool micbias1 = false;
  799. struct snd_soc_codec *codec = mbhc->codec;
  800. enum snd_jack_types jack_type;
  801. dev_dbg(codec->dev, "%s: enter\n", __func__);
  802. WCD_MBHC_RSC_LOCK(mbhc);
  803. mbhc->in_swch_irq_handler = true;
  804. /* cancel pending button press */
  805. if (wcd_cancel_btn_work(mbhc))
  806. pr_debug("%s: button press is canceled\n", __func__);
  807. WCD_MBHC_REG_READ(WCD_MBHC_MECH_DETECTION_TYPE, detection_type);
  808. /* Set the detection type appropriately */
  809. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  810. !detection_type);
  811. pr_debug("%s: mbhc->current_plug: %d detection_type: %d\n", __func__,
  812. mbhc->current_plug, detection_type);
  813. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug)
  814. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  815. &mbhc->correct_plug_swch);
  816. else
  817. pr_info("%s: hs_detect_plug work not cancelled\n", __func__);
  818. if (mbhc->mbhc_cb->micbias_enable_status)
  819. micbias1 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  820. MIC_BIAS_1);
  821. if ((mbhc->current_plug == MBHC_PLUG_TYPE_NONE) &&
  822. detection_type) {
  823. /* If moisture is present, then enable polling, disable
  824. * moisture detection and wait for interrupt
  825. */
  826. if (wcd_mbhc_moisture_detect(mbhc, detection_type))
  827. goto done;
  828. /* Make sure MASTER_BIAS_CTL is enabled */
  829. mbhc->mbhc_cb->mbhc_bias(codec, true);
  830. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  831. mbhc->mbhc_cb->mbhc_common_micb_ctrl(codec,
  832. MBHC_COMMON_MICB_TAIL_CURR, true);
  833. if (!mbhc->mbhc_cfg->hs_ext_micbias &&
  834. mbhc->mbhc_cb->micb_internal)
  835. /*
  836. * Enable Tx2 RBias if the headset
  837. * is using internal micbias
  838. */
  839. mbhc->mbhc_cb->micb_internal(codec, 1, true);
  840. /* Remove micbias pulldown */
  841. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 0);
  842. /* Apply trim if needed on the device */
  843. if (mbhc->mbhc_cb->trim_btn_reg)
  844. mbhc->mbhc_cb->trim_btn_reg(codec);
  845. /* Enable external voltage source to micbias if present */
  846. if (mbhc->mbhc_cb->enable_mb_source)
  847. mbhc->mbhc_cb->enable_mb_source(mbhc, true);
  848. mbhc->btn_press_intr = false;
  849. mbhc->is_btn_press = false;
  850. if (mbhc->mbhc_fn)
  851. mbhc->mbhc_fn->wcd_mbhc_detect_plug_type(mbhc);
  852. } else if ((mbhc->current_plug != MBHC_PLUG_TYPE_NONE)
  853. && !detection_type) {
  854. /* Disable external voltage source to micbias if present */
  855. if (mbhc->mbhc_cb->enable_mb_source)
  856. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  857. /* Disable HW FSM */
  858. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  859. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  860. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  861. mbhc->mbhc_cb->mbhc_common_micb_ctrl(codec,
  862. MBHC_COMMON_MICB_TAIL_CURR, false);
  863. if (mbhc->mbhc_cb->set_cap_mode)
  864. mbhc->mbhc_cb->set_cap_mode(codec, micbias1, false);
  865. mbhc->btn_press_intr = false;
  866. mbhc->is_btn_press = false;
  867. switch (mbhc->current_plug) {
  868. case MBHC_PLUG_TYPE_HEADPHONE:
  869. jack_type = SND_JACK_HEADPHONE;
  870. break;
  871. case MBHC_PLUG_TYPE_GND_MIC_SWAP:
  872. jack_type = SND_JACK_UNSUPPORTED;
  873. break;
  874. case MBHC_PLUG_TYPE_HEADSET:
  875. /* make sure to turn off Rbias */
  876. if (mbhc->mbhc_cb->micb_internal)
  877. mbhc->mbhc_cb->micb_internal(codec, 1, false);
  878. /* Pulldown micbias */
  879. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 1);
  880. jack_type = SND_JACK_HEADSET;
  881. break;
  882. case MBHC_PLUG_TYPE_HIGH_HPH:
  883. if (mbhc->mbhc_detection_logic == WCD_DETECTION_ADC)
  884. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  885. mbhc->is_extn_cable = false;
  886. jack_type = SND_JACK_LINEOUT;
  887. break;
  888. case MBHC_PLUG_TYPE_ANC_HEADPHONE:
  889. jack_type = SND_JACK_ANC_HEADPHONE;
  890. break;
  891. default:
  892. pr_info("%s: Invalid current plug: %d\n",
  893. __func__, mbhc->current_plug);
  894. jack_type = SND_JACK_UNSUPPORTED;
  895. break;
  896. }
  897. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM, false);
  898. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  899. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE, 1);
  900. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  901. mbhc->extn_cable_hph_rem = false;
  902. wcd_mbhc_report_plug(mbhc, 0, jack_type);
  903. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  904. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  905. if (mbhc->mbhc_cb->clk_setup)
  906. mbhc->mbhc_cb->clk_setup(mbhc->codec, false);
  907. }
  908. if (mbhc->mbhc_cfg->moisture_en &&
  909. mbhc->mbhc_cfg->moisture_duty_cycle_en) {
  910. if (mbhc->mbhc_cb->mbhc_moisture_polling_ctrl)
  911. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc,
  912. false);
  913. if (mbhc->mbhc_cb->mbhc_moisture_detect_en)
  914. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc,
  915. false);
  916. }
  917. } else if (!detection_type) {
  918. /* Disable external voltage source to micbias if present */
  919. if (mbhc->mbhc_cb->enable_mb_source)
  920. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  921. /* Disable HW FSM */
  922. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  923. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  924. mbhc->extn_cable_hph_rem = false;
  925. }
  926. done:
  927. mbhc->in_swch_irq_handler = false;
  928. WCD_MBHC_RSC_UNLOCK(mbhc);
  929. pr_debug("%s: leave\n", __func__);
  930. }
  931. static irqreturn_t wcd_mbhc_mech_plug_detect_irq(int irq, void *data)
  932. {
  933. int r = IRQ_HANDLED;
  934. struct wcd_mbhc *mbhc = data;
  935. pr_debug("%s: enter\n", __func__);
  936. if (unlikely((mbhc->mbhc_cb->lock_sleep(mbhc, true)) == false)) {
  937. pr_warn("%s: failed to hold suspend\n", __func__);
  938. r = IRQ_NONE;
  939. } else {
  940. /* Call handler */
  941. wcd_mbhc_swch_irq_handler(mbhc);
  942. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  943. }
  944. pr_debug("%s: leave %d\n", __func__, r);
  945. return r;
  946. }
  947. int wcd_mbhc_get_button_mask(struct wcd_mbhc *mbhc)
  948. {
  949. int mask = 0;
  950. int btn;
  951. btn = mbhc->mbhc_cb->map_btn_code_to_num(mbhc->codec);
  952. switch (btn) {
  953. case 0:
  954. mask = SND_JACK_BTN_0;
  955. break;
  956. case 1:
  957. mask = SND_JACK_BTN_1;
  958. break;
  959. case 2:
  960. mask = SND_JACK_BTN_2;
  961. break;
  962. case 3:
  963. mask = SND_JACK_BTN_3;
  964. break;
  965. case 4:
  966. mask = SND_JACK_BTN_4;
  967. break;
  968. case 5:
  969. mask = SND_JACK_BTN_5;
  970. break;
  971. default:
  972. break;
  973. }
  974. return mask;
  975. }
  976. EXPORT_SYMBOL(wcd_mbhc_get_button_mask);
  977. static void wcd_btn_lpress_fn(struct work_struct *work)
  978. {
  979. struct delayed_work *dwork;
  980. struct wcd_mbhc *mbhc;
  981. s16 btn_result = 0;
  982. pr_debug("%s: Enter\n", __func__);
  983. dwork = to_delayed_work(work);
  984. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_btn_dwork);
  985. WCD_MBHC_REG_READ(WCD_MBHC_BTN_RESULT, btn_result);
  986. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET) {
  987. pr_debug("%s: Reporting long button press event, btn_result: %d\n",
  988. __func__, btn_result);
  989. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  990. mbhc->buttons_pressed, mbhc->buttons_pressed);
  991. }
  992. pr_debug("%s: leave\n", __func__);
  993. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  994. }
  995. static bool wcd_mbhc_fw_validate(const void *data, size_t size)
  996. {
  997. u32 cfg_offset;
  998. struct wcd_mbhc_btn_detect_cfg *btn_cfg;
  999. struct firmware_cal fw;
  1000. fw.data = (void *)data;
  1001. fw.size = size;
  1002. if (fw.size < WCD_MBHC_CAL_MIN_SIZE)
  1003. return false;
  1004. /*
  1005. * Previous check guarantees that there is enough fw data up
  1006. * to num_btn
  1007. */
  1008. btn_cfg = WCD_MBHC_CAL_BTN_DET_PTR(fw.data);
  1009. cfg_offset = (u32) ((void *) btn_cfg - (void *) fw.data);
  1010. if (fw.size < (cfg_offset + WCD_MBHC_CAL_BTN_SZ(btn_cfg)))
  1011. return false;
  1012. return true;
  1013. }
  1014. static irqreturn_t wcd_mbhc_btn_press_handler(int irq, void *data)
  1015. {
  1016. struct wcd_mbhc *mbhc = data;
  1017. int mask;
  1018. unsigned long msec_val;
  1019. pr_debug("%s: enter\n", __func__);
  1020. complete(&mbhc->btn_press_compl);
  1021. WCD_MBHC_RSC_LOCK(mbhc);
  1022. wcd_cancel_btn_work(mbhc);
  1023. if (wcd_swch_level_remove(mbhc)) {
  1024. pr_debug("%s: Switch level is low ", __func__);
  1025. goto done;
  1026. }
  1027. mbhc->is_btn_press = true;
  1028. msec_val = jiffies_to_msecs(jiffies - mbhc->jiffies_atreport);
  1029. pr_debug("%s: msec_val = %ld\n", __func__, msec_val);
  1030. if (msec_val < MBHC_BUTTON_PRESS_THRESHOLD_MIN) {
  1031. pr_debug("%s: Too short, ignore button press\n", __func__);
  1032. goto done;
  1033. }
  1034. /* If switch interrupt already kicked in, ignore button press */
  1035. if (mbhc->in_swch_irq_handler) {
  1036. pr_debug("%s: Swtich level changed, ignore button press\n",
  1037. __func__);
  1038. goto done;
  1039. }
  1040. mask = wcd_mbhc_get_button_mask(mbhc);
  1041. if (mask == SND_JACK_BTN_0)
  1042. mbhc->btn_press_intr = true;
  1043. if (mbhc->current_plug != MBHC_PLUG_TYPE_HEADSET) {
  1044. pr_debug("%s: Plug isn't headset, ignore button press\n",
  1045. __func__);
  1046. goto done;
  1047. }
  1048. mbhc->buttons_pressed |= mask;
  1049. mbhc->mbhc_cb->lock_sleep(mbhc, true);
  1050. if (schedule_delayed_work(&mbhc->mbhc_btn_dwork,
  1051. msecs_to_jiffies(400)) == 0) {
  1052. WARN(1, "Button pressed twice without release event\n");
  1053. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1054. }
  1055. done:
  1056. pr_debug("%s: leave\n", __func__);
  1057. WCD_MBHC_RSC_UNLOCK(mbhc);
  1058. return IRQ_HANDLED;
  1059. }
  1060. static irqreturn_t wcd_mbhc_release_handler(int irq, void *data)
  1061. {
  1062. struct wcd_mbhc *mbhc = data;
  1063. int ret;
  1064. pr_debug("%s: enter\n", __func__);
  1065. WCD_MBHC_RSC_LOCK(mbhc);
  1066. if (wcd_swch_level_remove(mbhc)) {
  1067. pr_debug("%s: Switch level is low ", __func__);
  1068. goto exit;
  1069. }
  1070. if (mbhc->is_btn_press) {
  1071. mbhc->is_btn_press = false;
  1072. } else {
  1073. pr_debug("%s: This release is for fake btn press\n", __func__);
  1074. goto exit;
  1075. }
  1076. /*
  1077. * If current plug is headphone then there is no chance to
  1078. * get btn release interrupt, so connected cable should be
  1079. * headset not headphone.
  1080. * For ADC MBHC, ADC_COMPLETE interrupt will be generated
  1081. * in this case. So skip the check here.
  1082. */
  1083. if (mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY &&
  1084. mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  1085. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  1086. goto exit;
  1087. }
  1088. if (mbhc->buttons_pressed & WCD_MBHC_JACK_BUTTON_MASK) {
  1089. ret = wcd_cancel_btn_work(mbhc);
  1090. if (ret == 0) {
  1091. pr_debug("%s: Reporting long button release event\n",
  1092. __func__);
  1093. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  1094. 0, mbhc->buttons_pressed);
  1095. } else {
  1096. if (mbhc->in_swch_irq_handler) {
  1097. pr_debug("%s: Switch irq kicked in, ignore\n",
  1098. __func__);
  1099. } else {
  1100. pr_debug("%s: Reporting btn press\n",
  1101. __func__);
  1102. wcd_mbhc_jack_report(mbhc,
  1103. &mbhc->button_jack,
  1104. mbhc->buttons_pressed,
  1105. mbhc->buttons_pressed);
  1106. pr_debug("%s: Reporting btn release\n",
  1107. __func__);
  1108. wcd_mbhc_jack_report(mbhc,
  1109. &mbhc->button_jack,
  1110. 0, mbhc->buttons_pressed);
  1111. }
  1112. }
  1113. mbhc->buttons_pressed &= ~WCD_MBHC_JACK_BUTTON_MASK;
  1114. }
  1115. exit:
  1116. pr_debug("%s: leave\n", __func__);
  1117. WCD_MBHC_RSC_UNLOCK(mbhc);
  1118. return IRQ_HANDLED;
  1119. }
  1120. static irqreturn_t wcd_mbhc_hphl_ocp_irq(int irq, void *data)
  1121. {
  1122. struct wcd_mbhc *mbhc = data;
  1123. int val;
  1124. pr_debug("%s: received HPHL OCP irq\n", __func__);
  1125. if (mbhc) {
  1126. if (mbhc->mbhc_cb->hph_register_recovery) {
  1127. if (mbhc->mbhc_cb->hph_register_recovery(mbhc)) {
  1128. WCD_MBHC_REG_READ(WCD_MBHC_HPHR_OCP_STATUS,
  1129. val);
  1130. if ((val != -EINVAL) && val)
  1131. mbhc->is_hph_ocp_pending = true;
  1132. goto done;
  1133. }
  1134. }
  1135. if (mbhc->hphlocp_cnt < OCP_ATTEMPT) {
  1136. mbhc->hphlocp_cnt++;
  1137. pr_debug("%s: retry, hphlocp_cnt: %d\n", __func__,
  1138. mbhc->hphlocp_cnt);
  1139. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1140. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1141. } else {
  1142. mbhc->mbhc_cb->irq_control(mbhc->codec,
  1143. mbhc->intr_ids->hph_left_ocp,
  1144. false);
  1145. mbhc->hph_status |= SND_JACK_OC_HPHL;
  1146. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1147. mbhc->hph_status,
  1148. WCD_MBHC_JACK_MASK);
  1149. }
  1150. } else {
  1151. pr_err("%s: Bad wcd9xxx_spmi private data\n", __func__);
  1152. }
  1153. done:
  1154. return IRQ_HANDLED;
  1155. }
  1156. static irqreturn_t wcd_mbhc_hphr_ocp_irq(int irq, void *data)
  1157. {
  1158. struct wcd_mbhc *mbhc = data;
  1159. pr_debug("%s: received HPHR OCP irq\n", __func__);
  1160. if (!mbhc) {
  1161. pr_err("%s: Bad mbhc private data\n", __func__);
  1162. goto done;
  1163. }
  1164. if (mbhc->is_hph_ocp_pending) {
  1165. mbhc->is_hph_ocp_pending = false;
  1166. goto done;
  1167. }
  1168. if (mbhc->mbhc_cb->hph_register_recovery) {
  1169. if (mbhc->mbhc_cb->hph_register_recovery(mbhc))
  1170. /* register corruption, hence reset registers */
  1171. goto done;
  1172. }
  1173. if (mbhc->hphrocp_cnt < OCP_ATTEMPT) {
  1174. mbhc->hphrocp_cnt++;
  1175. pr_debug("%s: retry, hphrocp_cnt: %d\n", __func__,
  1176. mbhc->hphrocp_cnt);
  1177. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1178. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1179. } else {
  1180. mbhc->mbhc_cb->irq_control(mbhc->codec,
  1181. mbhc->intr_ids->hph_right_ocp,
  1182. false);
  1183. mbhc->hph_status |= SND_JACK_OC_HPHR;
  1184. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1185. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  1186. }
  1187. done:
  1188. return IRQ_HANDLED;
  1189. }
  1190. static int wcd_mbhc_initialise(struct wcd_mbhc *mbhc)
  1191. {
  1192. int ret = 0;
  1193. struct snd_soc_codec *codec = mbhc->codec;
  1194. pr_debug("%s: enter\n", __func__);
  1195. WCD_MBHC_RSC_LOCK(mbhc);
  1196. /* enable HS detection */
  1197. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1198. mbhc->mbhc_cb->hph_pull_up_control_v2(codec,
  1199. HS_PULLUP_I_DEFAULT);
  1200. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1201. mbhc->mbhc_cb->hph_pull_up_control(codec, I_DEFAULT);
  1202. else
  1203. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL, 3);
  1204. if (mbhc->mbhc_cfg->moisture_en && mbhc->mbhc_cb->mbhc_moisture_config
  1205. && !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  1206. mbhc->mbhc_cb->mbhc_moisture_config(mbhc);
  1207. /*
  1208. * For USB analog we need to override the switch configuration.
  1209. * Also, disable hph_l pull-up current source as HS_DET_L is driven
  1210. * by an external source
  1211. */
  1212. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1213. mbhc->hphl_swh = 0;
  1214. mbhc->gnd_swh = 0;
  1215. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1216. mbhc->mbhc_cb->hph_pull_up_control_v2(codec,
  1217. HS_PULLUP_I_OFF);
  1218. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1219. mbhc->mbhc_cb->hph_pull_up_control(codec, I_OFF);
  1220. else
  1221. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL,
  1222. 0);
  1223. }
  1224. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_PLUG_TYPE, mbhc->hphl_swh);
  1225. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_PLUG_TYPE, mbhc->gnd_swh);
  1226. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_SW_HPH_LP_100K_TO_GND, 1);
  1227. if (mbhc->mbhc_cfg->gnd_det_en && mbhc->mbhc_cb->mbhc_gnd_det_ctrl)
  1228. mbhc->mbhc_cb->mbhc_gnd_det_ctrl(codec, true);
  1229. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL, 1);
  1230. /*
  1231. * Disable L_DET for USB-C analog audio to avoid spurious interrupts
  1232. * when a non-audio accessory is inserted. L_DET_EN sets to 1 when FSA
  1233. * I2C driver notifies that ANALOG_AUDIO_ADAPTER is inserted
  1234. */
  1235. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1236. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  1237. else
  1238. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1239. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1240. /* Insertion debounce set to 48ms */
  1241. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 4);
  1242. } else {
  1243. /* Insertion debounce set to 96ms */
  1244. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 6);
  1245. }
  1246. /* Button Debounce set to 16ms */
  1247. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_DBNC, 2);
  1248. /* Enable micbias ramp */
  1249. if (mbhc->mbhc_cb->mbhc_micb_ramp_control)
  1250. mbhc->mbhc_cb->mbhc_micb_ramp_control(codec, true);
  1251. /* enable bias */
  1252. mbhc->mbhc_cb->mbhc_bias(codec, true);
  1253. /* enable MBHC clock */
  1254. if (mbhc->mbhc_cb->clk_setup) {
  1255. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1256. mbhc->mbhc_cb->clk_setup(codec, false);
  1257. else
  1258. mbhc->mbhc_cb->clk_setup(codec, true);
  1259. }
  1260. /* program HS_VREF value */
  1261. wcd_program_hs_vref(mbhc);
  1262. wcd_program_btn_threshold(mbhc, false);
  1263. reinit_completion(&mbhc->btn_press_compl);
  1264. WCD_MBHC_RSC_UNLOCK(mbhc);
  1265. pr_debug("%s: leave\n", __func__);
  1266. return ret;
  1267. }
  1268. static void wcd_mbhc_fw_read(struct work_struct *work)
  1269. {
  1270. struct delayed_work *dwork;
  1271. struct wcd_mbhc *mbhc;
  1272. struct snd_soc_codec *codec;
  1273. const struct firmware *fw;
  1274. struct firmware_cal *fw_data = NULL;
  1275. int ret = -1, retry = 0;
  1276. bool use_default_cal = false;
  1277. dwork = to_delayed_work(work);
  1278. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_firmware_dwork);
  1279. codec = mbhc->codec;
  1280. while (retry < FW_READ_ATTEMPTS) {
  1281. retry++;
  1282. pr_debug("%s:Attempt %d to request MBHC firmware\n",
  1283. __func__, retry);
  1284. if (mbhc->mbhc_cb->get_hwdep_fw_cal)
  1285. fw_data = mbhc->mbhc_cb->get_hwdep_fw_cal(mbhc,
  1286. WCD9XXX_MBHC_CAL);
  1287. if (!fw_data)
  1288. ret = request_firmware(&fw, "wcd9320/wcd9320_mbhc.bin",
  1289. codec->dev);
  1290. /*
  1291. * if request_firmware and hwdep cal both fail then
  1292. * sleep for 4sec for the userspace to send data to kernel
  1293. * retry for few times before bailing out
  1294. */
  1295. if ((ret != 0) && !fw_data) {
  1296. usleep_range(FW_READ_TIMEOUT, FW_READ_TIMEOUT +
  1297. WCD_MBHC_USLEEP_RANGE_MARGIN_US);
  1298. } else {
  1299. pr_debug("%s: MBHC Firmware read successful\n",
  1300. __func__);
  1301. break;
  1302. }
  1303. }
  1304. if (!fw_data)
  1305. pr_debug("%s: using request_firmware\n", __func__);
  1306. else
  1307. pr_debug("%s: using hwdep cal\n", __func__);
  1308. if (ret != 0 && !fw_data) {
  1309. pr_err("%s: Cannot load MBHC firmware use default cal\n",
  1310. __func__);
  1311. use_default_cal = true;
  1312. }
  1313. if (!use_default_cal) {
  1314. const void *data;
  1315. size_t size;
  1316. if (fw_data) {
  1317. data = fw_data->data;
  1318. size = fw_data->size;
  1319. } else {
  1320. data = fw->data;
  1321. size = fw->size;
  1322. }
  1323. if (wcd_mbhc_fw_validate(data, size) == false) {
  1324. pr_err("%s: Invalid MBHC cal data size use default cal\n",
  1325. __func__);
  1326. if (!fw_data)
  1327. release_firmware(fw);
  1328. } else {
  1329. if (fw_data) {
  1330. mbhc->mbhc_cfg->calibration =
  1331. (void *)fw_data->data;
  1332. mbhc->mbhc_cal = fw_data;
  1333. } else {
  1334. mbhc->mbhc_cfg->calibration =
  1335. (void *)fw->data;
  1336. mbhc->mbhc_fw = fw;
  1337. }
  1338. }
  1339. }
  1340. (void) wcd_mbhc_initialise(mbhc);
  1341. }
  1342. static int wcd_mbhc_set_keycode(struct wcd_mbhc *mbhc)
  1343. {
  1344. enum snd_jack_types type;
  1345. int i, ret, result = 0;
  1346. int *btn_key_code;
  1347. btn_key_code = mbhc->mbhc_cfg->key_code;
  1348. for (i = 0 ; i < WCD_MBHC_KEYCODE_NUM ; i++) {
  1349. if (btn_key_code[i] != 0) {
  1350. switch (i) {
  1351. case 0:
  1352. type = SND_JACK_BTN_0;
  1353. break;
  1354. case 1:
  1355. type = SND_JACK_BTN_1;
  1356. break;
  1357. case 2:
  1358. type = SND_JACK_BTN_2;
  1359. break;
  1360. case 3:
  1361. type = SND_JACK_BTN_3;
  1362. break;
  1363. case 4:
  1364. type = SND_JACK_BTN_4;
  1365. break;
  1366. case 5:
  1367. type = SND_JACK_BTN_5;
  1368. break;
  1369. default:
  1370. WARN_ONCE(1, "Wrong button number:%d\n", i);
  1371. result = -1;
  1372. return result;
  1373. }
  1374. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1375. type,
  1376. btn_key_code[i]);
  1377. if (ret) {
  1378. pr_err("%s: Failed to set code for %d\n",
  1379. __func__, btn_key_code[i]);
  1380. result = -1;
  1381. return result;
  1382. }
  1383. input_set_capability(
  1384. mbhc->button_jack.jack->input_dev,
  1385. EV_KEY, btn_key_code[i]);
  1386. pr_debug("%s: set btn%d key code:%d\n", __func__,
  1387. i, btn_key_code[i]);
  1388. }
  1389. }
  1390. if (btn_key_code[0])
  1391. mbhc->is_btn_already_regd = true;
  1392. return result;
  1393. }
  1394. static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
  1395. unsigned long mode, void *ptr)
  1396. {
  1397. struct wcd_mbhc *mbhc = container_of(nb, struct wcd_mbhc, fsa_nb);
  1398. if (!mbhc)
  1399. return -EINVAL;
  1400. dev_dbg(mbhc->codec->dev, "%s: mode = %lu\n", __func__, mode);
  1401. if (mode == POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER) {
  1402. if (mbhc->mbhc_cb->clk_setup)
  1403. mbhc->mbhc_cb->clk_setup(mbhc->codec, true);
  1404. /* insertion detected, enable L_DET_EN */
  1405. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1406. }
  1407. return 0;
  1408. }
  1409. int wcd_mbhc_start(struct wcd_mbhc *mbhc, struct wcd_mbhc_config *mbhc_cfg)
  1410. {
  1411. int rc = 0;
  1412. struct snd_soc_codec *codec;
  1413. struct snd_soc_card *card;
  1414. const char *usb_c_dt = "qcom,msm-mbhc-usbc-audio-supported";
  1415. if (!mbhc || !mbhc_cfg)
  1416. return -EINVAL;
  1417. codec = mbhc->codec;
  1418. card = codec->component.card;
  1419. /* update the mbhc config */
  1420. mbhc->mbhc_cfg = mbhc_cfg;
  1421. dev_dbg(mbhc->codec->dev, "%s: enter\n", __func__);
  1422. /* check if USB C analog is defined on device tree */
  1423. mbhc_cfg->enable_usbc_analog = 0;
  1424. if (of_find_property(card->dev->of_node, usb_c_dt, NULL)) {
  1425. rc = of_property_read_u32(card->dev->of_node, usb_c_dt,
  1426. &mbhc_cfg->enable_usbc_analog);
  1427. }
  1428. if (mbhc_cfg->enable_usbc_analog == 0 || rc != 0) {
  1429. dev_dbg(card->dev,
  1430. "%s: %s in dt node is missing or false\n",
  1431. __func__, usb_c_dt);
  1432. dev_dbg(card->dev,
  1433. "%s: skipping USB c analog configuration\n", __func__);
  1434. }
  1435. /* Parse fsa switch handle */
  1436. if (mbhc_cfg->enable_usbc_analog) {
  1437. dev_dbg(mbhc->codec->dev, "%s: usbc analog enabled\n",
  1438. __func__);
  1439. mbhc->swap_thr = GND_MIC_USBC_SWAP_THRESHOLD;
  1440. mbhc->fsa_np = of_parse_phandle(card->dev->of_node,
  1441. "fsa4480-i2c-handle", 0);
  1442. if (!mbhc->fsa_np) {
  1443. dev_err(card->dev, "%s: fsa4480 i2c node not found\n",
  1444. __func__);
  1445. rc = -EINVAL;
  1446. goto err;
  1447. }
  1448. }
  1449. /* Set btn key code */
  1450. if ((!mbhc->is_btn_already_regd) && wcd_mbhc_set_keycode(mbhc))
  1451. pr_err("Set btn key code error!!!\n");
  1452. if (!mbhc->mbhc_cfg->read_fw_bin ||
  1453. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_fw) ||
  1454. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_cal)) {
  1455. rc = wcd_mbhc_initialise(mbhc);
  1456. if (rc) {
  1457. dev_err(card->dev, "%s: wcd mbhc initialize failed\n",
  1458. __func__);
  1459. goto err;
  1460. }
  1461. } else {
  1462. if (!mbhc->mbhc_fw || !mbhc->mbhc_cal)
  1463. schedule_delayed_work(&mbhc->mbhc_firmware_dwork,
  1464. usecs_to_jiffies(FW_READ_TIMEOUT));
  1465. else
  1466. pr_err("%s: Skipping to read mbhc fw, 0x%pK %pK\n",
  1467. __func__, mbhc->mbhc_fw, mbhc->mbhc_cal);
  1468. }
  1469. if (mbhc_cfg->enable_usbc_analog) {
  1470. mbhc->fsa_nb.notifier_call = wcd_mbhc_usbc_ana_event_handler;
  1471. mbhc->fsa_nb.priority = 0;
  1472. rc = fsa4480_reg_notifier(&mbhc->fsa_nb, mbhc->fsa_np);
  1473. }
  1474. return rc;
  1475. err:
  1476. dev_dbg(mbhc->codec->dev, "%s: leave %d\n", __func__, rc);
  1477. return rc;
  1478. }
  1479. EXPORT_SYMBOL(wcd_mbhc_start);
  1480. void wcd_mbhc_stop(struct wcd_mbhc *mbhc)
  1481. {
  1482. pr_debug("%s: enter\n", __func__);
  1483. if (mbhc->current_plug != MBHC_PLUG_TYPE_NONE) {
  1484. if (mbhc->mbhc_cb && mbhc->mbhc_cb->skip_imped_detect)
  1485. mbhc->mbhc_cb->skip_imped_detect(mbhc->codec);
  1486. }
  1487. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1488. mbhc->hph_status = 0;
  1489. if (mbhc->mbhc_cb && mbhc->mbhc_cb->irq_control) {
  1490. mbhc->mbhc_cb->irq_control(mbhc->codec,
  1491. mbhc->intr_ids->hph_left_ocp,
  1492. false);
  1493. mbhc->mbhc_cb->irq_control(mbhc->codec,
  1494. mbhc->intr_ids->hph_right_ocp,
  1495. false);
  1496. }
  1497. if (mbhc->mbhc_fw || mbhc->mbhc_cal) {
  1498. cancel_delayed_work_sync(&mbhc->mbhc_firmware_dwork);
  1499. if (!mbhc->mbhc_cal)
  1500. release_firmware(mbhc->mbhc_fw);
  1501. mbhc->mbhc_fw = NULL;
  1502. mbhc->mbhc_cal = NULL;
  1503. }
  1504. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1505. fsa4480_unreg_notifier(&mbhc->fsa_nb, mbhc->fsa_np);
  1506. pr_debug("%s: leave\n", __func__);
  1507. }
  1508. EXPORT_SYMBOL(wcd_mbhc_stop);
  1509. /*
  1510. * wcd_mbhc_init : initialize MBHC internal structures.
  1511. *
  1512. * NOTE: mbhc->mbhc_cfg is not YET configure so shouldn't be used
  1513. */
  1514. int wcd_mbhc_init(struct wcd_mbhc *mbhc, struct snd_soc_codec *codec,
  1515. const struct wcd_mbhc_cb *mbhc_cb,
  1516. const struct wcd_mbhc_intr *mbhc_cdc_intr_ids,
  1517. struct wcd_mbhc_register *wcd_mbhc_regs,
  1518. bool impedance_det_en)
  1519. {
  1520. int ret = 0;
  1521. int hph_swh = 0;
  1522. int gnd_swh = 0;
  1523. u32 hph_moist_config[3];
  1524. struct snd_soc_card *card = codec->component.card;
  1525. const char *hph_switch = "qcom,msm-mbhc-hphl-swh";
  1526. const char *gnd_switch = "qcom,msm-mbhc-gnd-swh";
  1527. const char *hs_thre = "qcom,msm-mbhc-hs-mic-max-threshold-mv";
  1528. const char *hph_thre = "qcom,msm-mbhc-hs-mic-min-threshold-mv";
  1529. pr_debug("%s: enter\n", __func__);
  1530. ret = of_property_read_u32(card->dev->of_node, hph_switch, &hph_swh);
  1531. if (ret) {
  1532. dev_err(card->dev,
  1533. "%s: missing %s in dt node\n", __func__, hph_switch);
  1534. goto err;
  1535. }
  1536. ret = of_property_read_u32(card->dev->of_node, gnd_switch, &gnd_swh);
  1537. if (ret) {
  1538. dev_err(card->dev,
  1539. "%s: missing %s in dt node\n", __func__, gnd_switch);
  1540. goto err;
  1541. }
  1542. ret = of_property_read_u32(card->dev->of_node, hs_thre,
  1543. &(mbhc->hs_thr));
  1544. if (ret)
  1545. dev_dbg(card->dev,
  1546. "%s: missing %s in dt node\n", __func__, hs_thre);
  1547. ret = of_property_read_u32(card->dev->of_node, hph_thre,
  1548. &(mbhc->hph_thr));
  1549. if (ret)
  1550. dev_dbg(card->dev,
  1551. "%s: missing %s in dt node\n", __func__, hph_thre);
  1552. ret = of_property_read_u32_array(card->dev->of_node,
  1553. "qcom,msm-mbhc-moist-cfg",
  1554. hph_moist_config, 3);
  1555. if (ret) {
  1556. dev_dbg(card->dev, "%s: no qcom,msm-mbhc-moist-cfg in DT\n",
  1557. __func__);
  1558. mbhc->moist_vref = V_45_MV;
  1559. mbhc->moist_iref = I_3P0_UA;
  1560. mbhc->moist_rref = R_24_KOHM;
  1561. } else {
  1562. mbhc->moist_vref = hph_moist_config[0];
  1563. mbhc->moist_iref = hph_moist_config[1];
  1564. mbhc->moist_rref = hph_moist_config[2];
  1565. }
  1566. mbhc->in_swch_irq_handler = false;
  1567. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1568. mbhc->is_btn_press = false;
  1569. mbhc->codec = codec;
  1570. mbhc->intr_ids = mbhc_cdc_intr_ids;
  1571. mbhc->impedance_detect = impedance_det_en;
  1572. mbhc->hphl_swh = hph_swh;
  1573. mbhc->gnd_swh = gnd_swh;
  1574. mbhc->micbias_enable = false;
  1575. mbhc->mbhc_cb = mbhc_cb;
  1576. mbhc->btn_press_intr = false;
  1577. mbhc->is_hs_recording = false;
  1578. mbhc->is_extn_cable = false;
  1579. mbhc->extn_cable_hph_rem = false;
  1580. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  1581. mbhc->wcd_mbhc_regs = wcd_mbhc_regs;
  1582. mbhc->swap_thr = GND_MIC_SWAP_THRESHOLD;
  1583. if (mbhc->intr_ids == NULL) {
  1584. pr_err("%s: Interrupt mapping not provided\n", __func__);
  1585. return -EINVAL;
  1586. }
  1587. if (!mbhc->wcd_mbhc_regs) {
  1588. dev_err(codec->dev, "%s: mbhc registers are not defined\n",
  1589. __func__);
  1590. return -EINVAL;
  1591. }
  1592. /* Check if IRQ and other required callbacks are defined or not */
  1593. if (!mbhc_cb || !mbhc_cb->request_irq || !mbhc_cb->irq_control ||
  1594. !mbhc_cb->free_irq || !mbhc_cb->map_btn_code_to_num ||
  1595. !mbhc_cb->lock_sleep || !mbhc_cb->mbhc_bias ||
  1596. !mbhc_cb->set_btn_thr) {
  1597. dev_err(codec->dev, "%s: required mbhc callbacks are not defined\n",
  1598. __func__);
  1599. return -EINVAL;
  1600. }
  1601. /* No need to create new sound card jacks if is is already created */
  1602. if (mbhc->headset_jack.jack == NULL) {
  1603. ret = snd_soc_card_jack_new(codec->component.card,
  1604. "Headset Jack", WCD_MBHC_JACK_MASK,
  1605. &mbhc->headset_jack, NULL, 0);
  1606. if (ret) {
  1607. pr_err("%s: Failed to create new jack\n", __func__);
  1608. return ret;
  1609. }
  1610. ret = snd_soc_card_jack_new(codec->component.card,
  1611. "Button Jack",
  1612. WCD_MBHC_JACK_BUTTON_MASK,
  1613. &mbhc->button_jack, NULL, 0);
  1614. if (ret) {
  1615. pr_err("Failed to create new jack\n");
  1616. return ret;
  1617. }
  1618. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1619. SND_JACK_BTN_0,
  1620. KEY_MEDIA);
  1621. if (ret) {
  1622. pr_err("%s: Failed to set code for btn-0\n",
  1623. __func__);
  1624. return ret;
  1625. }
  1626. INIT_DELAYED_WORK(&mbhc->mbhc_firmware_dwork,
  1627. wcd_mbhc_fw_read);
  1628. INIT_DELAYED_WORK(&mbhc->mbhc_btn_dwork, wcd_btn_lpress_fn);
  1629. }
  1630. mutex_init(&mbhc->hphl_pa_lock);
  1631. mutex_init(&mbhc->hphr_pa_lock);
  1632. init_completion(&mbhc->btn_press_compl);
  1633. /* Register event notifier */
  1634. mbhc->nblock.notifier_call = wcd_event_notify;
  1635. if (mbhc->mbhc_cb->register_notifier) {
  1636. ret = mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock,
  1637. true);
  1638. if (ret) {
  1639. pr_err("%s: Failed to register notifier %d\n",
  1640. __func__, ret);
  1641. return ret;
  1642. }
  1643. }
  1644. init_waitqueue_head(&mbhc->wait_btn_press);
  1645. mutex_init(&mbhc->codec_resource_lock);
  1646. switch (mbhc->mbhc_detection_logic) {
  1647. case WCD_DETECTION_LEGACY:
  1648. wcd_mbhc_legacy_init(mbhc);
  1649. break;
  1650. case WCD_DETECTION_ADC:
  1651. wcd_mbhc_adc_init(mbhc);
  1652. break;
  1653. default:
  1654. pr_err("%s: Unknown detection logic type %d\n",
  1655. __func__, mbhc->mbhc_detection_logic);
  1656. break;
  1657. }
  1658. if (!mbhc->mbhc_fn ||
  1659. !mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq ||
  1660. !mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq ||
  1661. !mbhc->mbhc_fn->wcd_mbhc_detect_plug_type ||
  1662. !mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1663. pr_err("%s: mbhc function pointer is NULL\n", __func__);
  1664. goto err_mbhc_sw_irq;
  1665. }
  1666. ret = mbhc->mbhc_cb->request_irq(codec, mbhc->intr_ids->mbhc_sw_intr,
  1667. wcd_mbhc_mech_plug_detect_irq,
  1668. "mbhc sw intr", mbhc);
  1669. if (ret) {
  1670. pr_err("%s: Failed to request irq %d, ret = %d\n", __func__,
  1671. mbhc->intr_ids->mbhc_sw_intr, ret);
  1672. goto err_mbhc_sw_irq;
  1673. }
  1674. ret = mbhc->mbhc_cb->request_irq(codec,
  1675. mbhc->intr_ids->mbhc_btn_press_intr,
  1676. wcd_mbhc_btn_press_handler,
  1677. "Button Press detect", mbhc);
  1678. if (ret) {
  1679. pr_err("%s: Failed to request irq %d\n", __func__,
  1680. mbhc->intr_ids->mbhc_btn_press_intr);
  1681. goto err_btn_press_irq;
  1682. }
  1683. ret = mbhc->mbhc_cb->request_irq(codec,
  1684. mbhc->intr_ids->mbhc_btn_release_intr,
  1685. wcd_mbhc_release_handler,
  1686. "Button Release detect", mbhc);
  1687. if (ret) {
  1688. pr_err("%s: Failed to request irq %d\n", __func__,
  1689. mbhc->intr_ids->mbhc_btn_release_intr);
  1690. goto err_btn_release_irq;
  1691. }
  1692. ret = mbhc->mbhc_cb->request_irq(codec,
  1693. mbhc->intr_ids->mbhc_hs_ins_intr,
  1694. mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq,
  1695. "Elect Insert", mbhc);
  1696. if (ret) {
  1697. pr_err("%s: Failed to request irq %d\n", __func__,
  1698. mbhc->intr_ids->mbhc_hs_ins_intr);
  1699. goto err_mbhc_hs_ins_irq;
  1700. }
  1701. mbhc->mbhc_cb->irq_control(codec, mbhc->intr_ids->mbhc_hs_ins_intr,
  1702. false);
  1703. clear_bit(WCD_MBHC_ELEC_HS_INS, &mbhc->intr_status);
  1704. ret = mbhc->mbhc_cb->request_irq(codec,
  1705. mbhc->intr_ids->mbhc_hs_rem_intr,
  1706. mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq,
  1707. "Elect Remove", mbhc);
  1708. if (ret) {
  1709. pr_err("%s: Failed to request irq %d\n", __func__,
  1710. mbhc->intr_ids->mbhc_hs_rem_intr);
  1711. goto err_mbhc_hs_rem_irq;
  1712. }
  1713. mbhc->mbhc_cb->irq_control(codec, mbhc->intr_ids->mbhc_hs_rem_intr,
  1714. false);
  1715. clear_bit(WCD_MBHC_ELEC_HS_REM, &mbhc->intr_status);
  1716. ret = mbhc->mbhc_cb->request_irq(codec, mbhc->intr_ids->hph_left_ocp,
  1717. wcd_mbhc_hphl_ocp_irq, "HPH_L OCP detect",
  1718. mbhc);
  1719. if (ret) {
  1720. pr_err("%s: Failed to request irq %d\n", __func__,
  1721. mbhc->intr_ids->hph_left_ocp);
  1722. goto err_hphl_ocp_irq;
  1723. }
  1724. ret = mbhc->mbhc_cb->request_irq(codec, mbhc->intr_ids->hph_right_ocp,
  1725. wcd_mbhc_hphr_ocp_irq, "HPH_R OCP detect",
  1726. mbhc);
  1727. if (ret) {
  1728. pr_err("%s: Failed to request irq %d\n", __func__,
  1729. mbhc->intr_ids->hph_right_ocp);
  1730. goto err_hphr_ocp_irq;
  1731. }
  1732. mbhc->deinit_in_progress = false;
  1733. pr_debug("%s: leave ret %d\n", __func__, ret);
  1734. return ret;
  1735. err_hphr_ocp_irq:
  1736. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->hph_left_ocp, mbhc);
  1737. err_hphl_ocp_irq:
  1738. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_hs_rem_intr, mbhc);
  1739. err_mbhc_hs_rem_irq:
  1740. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_hs_ins_intr, mbhc);
  1741. err_mbhc_hs_ins_irq:
  1742. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_btn_release_intr,
  1743. mbhc);
  1744. err_btn_release_irq:
  1745. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_btn_press_intr,
  1746. mbhc);
  1747. err_btn_press_irq:
  1748. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1749. err_mbhc_sw_irq:
  1750. if (mbhc->mbhc_cb->register_notifier)
  1751. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1752. mutex_destroy(&mbhc->codec_resource_lock);
  1753. err:
  1754. pr_debug("%s: leave ret %d\n", __func__, ret);
  1755. return ret;
  1756. }
  1757. EXPORT_SYMBOL(wcd_mbhc_init);
  1758. void wcd_mbhc_deinit(struct wcd_mbhc *mbhc)
  1759. {
  1760. struct snd_soc_codec *codec = mbhc->codec;
  1761. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1762. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_btn_press_intr,
  1763. mbhc);
  1764. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_btn_release_intr,
  1765. mbhc);
  1766. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_hs_ins_intr, mbhc);
  1767. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_hs_rem_intr, mbhc);
  1768. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->hph_left_ocp, mbhc);
  1769. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->hph_right_ocp, mbhc);
  1770. if (mbhc->mbhc_cb && mbhc->mbhc_cb->register_notifier)
  1771. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1772. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1773. WCD_MBHC_RSC_LOCK(mbhc);
  1774. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  1775. &mbhc->correct_plug_swch);
  1776. WCD_MBHC_RSC_UNLOCK(mbhc);
  1777. }
  1778. mutex_destroy(&mbhc->codec_resource_lock);
  1779. mutex_destroy(&mbhc->hphl_pa_lock);
  1780. mutex_destroy(&mbhc->hphr_pa_lock);
  1781. }
  1782. EXPORT_SYMBOL(wcd_mbhc_deinit);
  1783. static int __init mbhc_init(void)
  1784. {
  1785. return 0;
  1786. }
  1787. static void __exit mbhc_exit(void)
  1788. {
  1789. }
  1790. module_init(mbhc_init);
  1791. module_exit(mbhc_exit);
  1792. MODULE_DESCRIPTION("wcd MBHC v2 module");
  1793. MODULE_LICENSE("GPL v2");