wcd-mbhc-v2.c 66 KB

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