wcd-mbhc-v2.c 60 KB

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