wcd-mbhc-v2.c 58 KB

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