wcd-mbhc-v2.c 60 KB

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