wcd-mbhc-v2.c 62 KB

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