wcd-mbhc-v2.c 59 KB

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