wcd-mbhc-v2.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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 snd_soc_component *component, bool connect)
  883. {
  884. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  885. if (connect) {
  886. if (of_find_property(component->card->dev->of_node,
  887. "qcom,usbss-hsj-connect-enabled", NULL))
  888. wcd_usbss_switch_update(WCD_USBSS_HSJ_CONNECT, WCD_USBSS_CABLE_CONNECT);
  889. } else {
  890. if (of_find_property(component->card->dev->of_node,
  891. "qcom,usbss-hsj-connect-enabled", NULL))
  892. wcd_usbss_switch_update(WCD_USBSS_HSJ_CONNECT, WCD_USBSS_CABLE_DISCONNECT);
  893. }
  894. #endif
  895. }
  896. static void wcd_mbhc_swch_irq_handler(struct wcd_mbhc *mbhc)
  897. {
  898. bool detection_type = 0;
  899. bool micbias1 = false;
  900. struct snd_soc_component *component = mbhc->component;
  901. enum snd_jack_types jack_type;
  902. int extdev_type = 0;
  903. dev_dbg(component->dev, "%s: enter\n", __func__);
  904. WCD_MBHC_RSC_LOCK(mbhc);
  905. mbhc->in_swch_irq_handler = true;
  906. /* cancel pending button press */
  907. if (wcd_cancel_btn_work(mbhc))
  908. pr_debug("%s: button press is canceled\n", __func__);
  909. WCD_MBHC_REG_READ(WCD_MBHC_MECH_DETECTION_TYPE, detection_type);
  910. /* Set the detection type appropriately */
  911. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  912. !detection_type);
  913. pr_debug("%s: mbhc->current_plug: %d detection_type: %d\n", __func__,
  914. mbhc->current_plug, detection_type);
  915. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug)
  916. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  917. &mbhc->correct_plug_swch);
  918. else
  919. pr_info("%s: hs_detect_plug work not cancelled\n", __func__);
  920. /* Enable micbias ramp */
  921. if (mbhc->mbhc_cb->mbhc_micb_ramp_control)
  922. mbhc->mbhc_cb->mbhc_micb_ramp_control(component, true);
  923. if (mbhc->mbhc_cb->micbias_enable_status)
  924. micbias1 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  925. MIC_BIAS_1);
  926. if ((mbhc->current_plug == MBHC_PLUG_TYPE_NONE) &&
  927. detection_type) {
  928. wcd_mbhc_set_hsj_connect(component, 1);
  929. /* If moisture is present, then enable polling, disable
  930. * moisture detection and wait for interrupt
  931. */
  932. if (wcd_mbhc_moisture_detect(mbhc, detection_type))
  933. goto done;
  934. /* Make sure MASTER_BIAS_CTL is enabled */
  935. mbhc->mbhc_cb->mbhc_bias(component, true);
  936. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  937. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  938. MBHC_COMMON_MICB_TAIL_CURR, true);
  939. if (!mbhc->mbhc_cfg->hs_ext_micbias &&
  940. mbhc->mbhc_cb->micb_internal)
  941. /*
  942. * Enable Tx2 RBias if the headset
  943. * is using internal micbias
  944. */
  945. mbhc->mbhc_cb->micb_internal(component, 1, true);
  946. /* Remove micbias pulldown */
  947. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 0);
  948. /* Apply trim if needed on the device */
  949. if (mbhc->mbhc_cb->trim_btn_reg)
  950. mbhc->mbhc_cb->trim_btn_reg(component);
  951. /* Enable external voltage source to micbias if present */
  952. if (mbhc->mbhc_cb->enable_mb_source)
  953. mbhc->mbhc_cb->enable_mb_source(mbhc, true);
  954. mbhc->btn_press_intr = false;
  955. mbhc->is_btn_press = false;
  956. if (mbhc->mbhc_fn)
  957. mbhc->mbhc_fn->wcd_mbhc_detect_plug_type(mbhc);
  958. } else if ((mbhc->current_plug != MBHC_PLUG_TYPE_NONE)
  959. && !detection_type) {
  960. /* Disable external voltage source to micbias if present */
  961. if (mbhc->mbhc_cb->enable_mb_source)
  962. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  963. /* Disable HW FSM */
  964. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  965. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  966. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  967. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  968. MBHC_COMMON_MICB_TAIL_CURR, false);
  969. if (mbhc->mbhc_cb->set_cap_mode)
  970. mbhc->mbhc_cb->set_cap_mode(component, micbias1, false);
  971. mbhc->btn_press_intr = false;
  972. mbhc->is_btn_press = false;
  973. switch (mbhc->current_plug) {
  974. case MBHC_PLUG_TYPE_HEADPHONE:
  975. jack_type = SND_JACK_HEADPHONE;
  976. extdev_type = EXTCON_JACK_HEADPHONE;
  977. break;
  978. case MBHC_PLUG_TYPE_GND_MIC_SWAP:
  979. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  980. jack_type = SND_JACK_UNSUPPORTED;
  981. #else
  982. jack_type = SND_JACK_HEADPHONE;
  983. #endif /* CONFIG_AUDIO_QGKI */
  984. extdev_type = EXTCON_MECHANICAL;
  985. break;
  986. case MBHC_PLUG_TYPE_HEADSET:
  987. /* make sure to turn off Rbias */
  988. if (mbhc->mbhc_cb->micb_internal)
  989. mbhc->mbhc_cb->micb_internal(component,
  990. 1, false);
  991. /* Pulldown micbias */
  992. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 1);
  993. jack_type = SND_JACK_HEADSET;
  994. extdev_type = EXTCON_JACK_MICROPHONE;
  995. break;
  996. case MBHC_PLUG_TYPE_HIGH_HPH:
  997. if (mbhc->mbhc_detection_logic == WCD_DETECTION_ADC)
  998. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  999. mbhc->is_extn_cable = false;
  1000. jack_type = SND_JACK_LINEOUT;
  1001. extdev_type = EXTCON_JACK_LINE_OUT;
  1002. break;
  1003. default:
  1004. pr_info("%s: Invalid current plug: %d\n",
  1005. __func__, mbhc->current_plug);
  1006. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1007. jack_type = SND_JACK_UNSUPPORTED;
  1008. #else
  1009. jack_type = SND_JACK_HEADPHONE;
  1010. #endif /* CONFIG_AUDIO_QGKI */
  1011. extdev_type = EXTCON_MECHANICAL;
  1012. break;
  1013. }
  1014. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM, false);
  1015. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  1016. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE, 1);
  1017. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  1018. mbhc->extn_cable_hph_rem = false;
  1019. wcd_mbhc_report_plug(mbhc, 0, jack_type);
  1020. extcon_set_state_sync(mbhc->extdev, extdev_type, 0);
  1021. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1022. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  1023. if (mbhc->mbhc_cb->clk_setup)
  1024. mbhc->mbhc_cb->clk_setup(
  1025. mbhc->component, false);
  1026. }
  1027. if (mbhc->mbhc_cfg->moisture_en ||
  1028. mbhc->mbhc_cfg->moisture_duty_cycle_en) {
  1029. if (mbhc->mbhc_cb->mbhc_moisture_polling_ctrl)
  1030. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc,
  1031. false);
  1032. if (mbhc->mbhc_cb->mbhc_moisture_detect_en)
  1033. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc,
  1034. false);
  1035. }
  1036. wcd_mbhc_set_hsj_connect(component, 0);
  1037. } else if (!detection_type) {
  1038. /* Disable external voltage source to micbias if present */
  1039. if (mbhc->mbhc_cb->enable_mb_source)
  1040. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  1041. /* Disable HW FSM */
  1042. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  1043. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  1044. mbhc->extn_cable_hph_rem = false;
  1045. }
  1046. done:
  1047. mbhc->in_swch_irq_handler = false;
  1048. WCD_MBHC_RSC_UNLOCK(mbhc);
  1049. pr_debug("%s: leave\n", __func__);
  1050. }
  1051. static irqreturn_t wcd_mbhc_mech_plug_detect_irq(int irq, void *data)
  1052. {
  1053. int r = IRQ_HANDLED;
  1054. struct wcd_mbhc *mbhc = data;
  1055. pr_debug("%s: enter\n", __func__);
  1056. if (mbhc == NULL) {
  1057. pr_err("%s: NULL irq data\n", __func__);
  1058. return IRQ_NONE;
  1059. }
  1060. /* WCD USB AATC did not required mech plug detection, will receive
  1061. * insertion/removal events from UCSI layer
  1062. */
  1063. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1064. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1065. pr_debug("%s: leave, (irq_none)", __func__);
  1066. return IRQ_NONE;
  1067. }
  1068. #endif
  1069. if (unlikely((mbhc->mbhc_cb->lock_sleep(mbhc, true)) == false)) {
  1070. pr_warn("%s: failed to hold suspend\n", __func__);
  1071. r = IRQ_NONE;
  1072. } else {
  1073. /* Call handler */
  1074. wcd_mbhc_swch_irq_handler(mbhc);
  1075. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1076. }
  1077. pr_debug("%s: leave %d\n", __func__, r);
  1078. return r;
  1079. }
  1080. int wcd_mbhc_get_button_mask(struct wcd_mbhc *mbhc)
  1081. {
  1082. int mask = 0;
  1083. int btn;
  1084. btn = mbhc->mbhc_cb->map_btn_code_to_num(mbhc->component);
  1085. switch (btn) {
  1086. case 0:
  1087. mask = SND_JACK_BTN_0;
  1088. break;
  1089. case 1:
  1090. mask = SND_JACK_BTN_1;
  1091. break;
  1092. case 2:
  1093. mask = SND_JACK_BTN_2;
  1094. break;
  1095. case 3:
  1096. mask = SND_JACK_BTN_3;
  1097. break;
  1098. case 4:
  1099. mask = SND_JACK_BTN_4;
  1100. break;
  1101. case 5:
  1102. mask = SND_JACK_BTN_5;
  1103. break;
  1104. default:
  1105. break;
  1106. }
  1107. return mask;
  1108. }
  1109. EXPORT_SYMBOL(wcd_mbhc_get_button_mask);
  1110. static void wcd_btn_lpress_fn(struct work_struct *work)
  1111. {
  1112. struct delayed_work *dwork;
  1113. struct wcd_mbhc *mbhc;
  1114. s16 btn_result = 0;
  1115. pr_debug("%s: Enter\n", __func__);
  1116. dwork = to_delayed_work(work);
  1117. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_btn_dwork);
  1118. WCD_MBHC_REG_READ(WCD_MBHC_BTN_RESULT, btn_result);
  1119. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET) {
  1120. pr_debug("%s: Reporting long button press event, btn_result: %d\n",
  1121. __func__, btn_result);
  1122. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  1123. mbhc->buttons_pressed, mbhc->buttons_pressed);
  1124. }
  1125. pr_debug("%s: leave\n", __func__);
  1126. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1127. }
  1128. static bool wcd_mbhc_fw_validate(const void *data, size_t size)
  1129. {
  1130. u32 cfg_offset;
  1131. struct wcd_mbhc_btn_detect_cfg *btn_cfg;
  1132. struct firmware_cal fw;
  1133. fw.data = (void *)data;
  1134. fw.size = size;
  1135. if (fw.size < WCD_MBHC_CAL_MIN_SIZE)
  1136. return false;
  1137. /*
  1138. * Previous check guarantees that there is enough fw data up
  1139. * to num_btn
  1140. */
  1141. btn_cfg = WCD_MBHC_CAL_BTN_DET_PTR(fw.data);
  1142. cfg_offset = (u32) ((void *) btn_cfg - (void *) fw.data);
  1143. if (fw.size < (cfg_offset + WCD_MBHC_CAL_BTN_SZ(btn_cfg)))
  1144. return false;
  1145. return true;
  1146. }
  1147. static irqreturn_t wcd_mbhc_btn_press_handler(int irq, void *data)
  1148. {
  1149. struct wcd_mbhc *mbhc = data;
  1150. int mask;
  1151. unsigned long msec_val;
  1152. pr_debug("%s: enter\n", __func__);
  1153. complete(&mbhc->btn_press_compl);
  1154. WCD_MBHC_RSC_LOCK(mbhc);
  1155. wcd_cancel_btn_work(mbhc);
  1156. if (wcd_swch_level_remove(mbhc)) {
  1157. pr_debug("%s: Switch level is low ", __func__);
  1158. goto done;
  1159. }
  1160. mbhc->is_btn_press = true;
  1161. msec_val = jiffies_to_msecs(jiffies - mbhc->jiffies_atreport);
  1162. pr_debug("%s: msec_val = %ld\n", __func__, msec_val);
  1163. if (msec_val < MBHC_BUTTON_PRESS_THRESHOLD_MIN) {
  1164. pr_debug("%s: Too short, ignore button press\n", __func__);
  1165. goto done;
  1166. }
  1167. /* If switch interrupt already kicked in, ignore button press */
  1168. if (mbhc->in_swch_irq_handler) {
  1169. pr_debug("%s: Swtich level changed, ignore button press\n",
  1170. __func__);
  1171. goto done;
  1172. }
  1173. mask = wcd_mbhc_get_button_mask(mbhc);
  1174. if (mask == SND_JACK_BTN_0)
  1175. mbhc->btn_press_intr = true;
  1176. if (mbhc->current_plug != MBHC_PLUG_TYPE_HEADSET) {
  1177. pr_debug("%s: Plug isn't headset, ignore button press\n",
  1178. __func__);
  1179. goto done;
  1180. }
  1181. mbhc->buttons_pressed |= mask;
  1182. mbhc->mbhc_cb->lock_sleep(mbhc, true);
  1183. if (schedule_delayed_work(&mbhc->mbhc_btn_dwork,
  1184. msecs_to_jiffies(400)) == 0) {
  1185. WARN(1, "Button pressed twice without release event\n");
  1186. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1187. }
  1188. done:
  1189. pr_debug("%s: leave\n", __func__);
  1190. WCD_MBHC_RSC_UNLOCK(mbhc);
  1191. return IRQ_HANDLED;
  1192. }
  1193. static irqreturn_t wcd_mbhc_release_handler(int irq, void *data)
  1194. {
  1195. struct wcd_mbhc *mbhc = data;
  1196. int ret;
  1197. pr_debug("%s: enter\n", __func__);
  1198. WCD_MBHC_RSC_LOCK(mbhc);
  1199. if (wcd_swch_level_remove(mbhc)) {
  1200. pr_debug("%s: Switch level is low ", __func__);
  1201. goto exit;
  1202. }
  1203. if (mbhc->is_btn_press) {
  1204. mbhc->is_btn_press = false;
  1205. } else {
  1206. pr_debug("%s: This release is for fake btn press\n", __func__);
  1207. goto exit;
  1208. }
  1209. /*
  1210. * If current plug is headphone then there is no chance to
  1211. * get btn release interrupt, so connected cable should be
  1212. * headset not headphone.
  1213. * For ADC MBHC, ADC_COMPLETE interrupt will be generated
  1214. * in this case. So skip the check here.
  1215. */
  1216. if (mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY &&
  1217. mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  1218. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  1219. goto exit;
  1220. }
  1221. if (mbhc->buttons_pressed & WCD_MBHC_JACK_BUTTON_MASK) {
  1222. ret = wcd_cancel_btn_work(mbhc);
  1223. if (ret == 0) {
  1224. pr_debug("%s: Reporting long button release event\n",
  1225. __func__);
  1226. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  1227. 0, mbhc->buttons_pressed);
  1228. } else {
  1229. if (mbhc->in_swch_irq_handler) {
  1230. pr_debug("%s: Switch irq kicked in, ignore\n",
  1231. __func__);
  1232. } else {
  1233. pr_debug("%s: Reporting btn press\n",
  1234. __func__);
  1235. wcd_mbhc_jack_report(mbhc,
  1236. &mbhc->button_jack,
  1237. mbhc->buttons_pressed,
  1238. mbhc->buttons_pressed);
  1239. pr_debug("%s: Reporting btn release\n",
  1240. __func__);
  1241. wcd_mbhc_jack_report(mbhc,
  1242. &mbhc->button_jack,
  1243. 0, mbhc->buttons_pressed);
  1244. }
  1245. }
  1246. mbhc->buttons_pressed &= ~WCD_MBHC_JACK_BUTTON_MASK;
  1247. }
  1248. exit:
  1249. pr_debug("%s: leave\n", __func__);
  1250. WCD_MBHC_RSC_UNLOCK(mbhc);
  1251. return IRQ_HANDLED;
  1252. }
  1253. static irqreturn_t wcd_mbhc_hphl_ocp_irq(int irq, void *data)
  1254. {
  1255. struct wcd_mbhc *mbhc = data;
  1256. int val;
  1257. pr_debug("%s: received HPHL OCP irq\n", __func__);
  1258. if (mbhc) {
  1259. if (mbhc->mbhc_cb->hph_register_recovery) {
  1260. if (mbhc->mbhc_cb->hph_register_recovery(mbhc)) {
  1261. WCD_MBHC_REG_READ(WCD_MBHC_HPHR_OCP_STATUS,
  1262. val);
  1263. if ((val != -EINVAL) && val)
  1264. mbhc->is_hph_ocp_pending = true;
  1265. goto done;
  1266. }
  1267. }
  1268. if (mbhc->hphlocp_cnt < OCP_ATTEMPT) {
  1269. mbhc->hphlocp_cnt++;
  1270. pr_debug("%s: retry, hphlocp_cnt: %d\n", __func__,
  1271. mbhc->hphlocp_cnt);
  1272. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1273. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1274. } else {
  1275. mbhc->mbhc_cb->irq_control(mbhc->component,
  1276. mbhc->intr_ids->hph_left_ocp,
  1277. false);
  1278. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1279. mbhc->hph_status |= SND_JACK_OC_HPHL;
  1280. #endif /* CONFIG_AUDIO_QGKI */
  1281. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1282. mbhc->hph_status,
  1283. WCD_MBHC_JACK_MASK);
  1284. }
  1285. } else {
  1286. pr_err("%s: Bad wcd9xxx_spmi private data\n", __func__);
  1287. }
  1288. done:
  1289. return IRQ_HANDLED;
  1290. }
  1291. static irqreturn_t wcd_mbhc_hphr_ocp_irq(int irq, void *data)
  1292. {
  1293. struct wcd_mbhc *mbhc = data;
  1294. pr_debug("%s: received HPHR OCP irq\n", __func__);
  1295. if (!mbhc) {
  1296. pr_err("%s: Bad mbhc private data\n", __func__);
  1297. goto done;
  1298. }
  1299. if (mbhc->is_hph_ocp_pending) {
  1300. mbhc->is_hph_ocp_pending = false;
  1301. goto done;
  1302. }
  1303. if (mbhc->mbhc_cb->hph_register_recovery) {
  1304. if (mbhc->mbhc_cb->hph_register_recovery(mbhc))
  1305. /* register corruption, hence reset registers */
  1306. goto done;
  1307. }
  1308. if (mbhc->hphrocp_cnt < OCP_ATTEMPT) {
  1309. mbhc->hphrocp_cnt++;
  1310. pr_debug("%s: retry, hphrocp_cnt: %d\n", __func__,
  1311. mbhc->hphrocp_cnt);
  1312. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1313. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1314. } else {
  1315. mbhc->mbhc_cb->irq_control(mbhc->component,
  1316. mbhc->intr_ids->hph_right_ocp,
  1317. false);
  1318. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1319. mbhc->hph_status |= SND_JACK_OC_HPHR;
  1320. #endif /* CONFIG_AUDIO_QGKI */
  1321. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1322. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  1323. }
  1324. done:
  1325. return IRQ_HANDLED;
  1326. }
  1327. static int wcd_mbhc_initialise(struct wcd_mbhc *mbhc)
  1328. {
  1329. int ret = 0;
  1330. struct snd_soc_component *component = mbhc->component;
  1331. pr_debug("%s: enter\n", __func__);
  1332. WCD_MBHC_RSC_LOCK(mbhc);
  1333. /* enable HS detection */
  1334. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1335. mbhc->mbhc_cb->hph_pull_up_control_v2(component,
  1336. HS_PULLUP_I_DEFAULT);
  1337. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1338. mbhc->mbhc_cb->hph_pull_up_control(component, I_DEFAULT);
  1339. else
  1340. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL, 3);
  1341. /* Configure for moisture detection when duty cycle is not enabled.
  1342. * Otherwise disable moisture detection.
  1343. */
  1344. if (mbhc->mbhc_cfg->moisture_en && mbhc->mbhc_cb->mbhc_moisture_config
  1345. && !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  1346. mbhc->mbhc_cb->mbhc_moisture_config(mbhc);
  1347. else if (mbhc->mbhc_cb->mbhc_moisture_detect_en)
  1348. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc, false);
  1349. /*
  1350. * For USB analog we need to override the switch configuration.
  1351. * Also, disable hph_l pull-up current source as HS_DET_L is driven
  1352. * by an external source
  1353. */
  1354. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1355. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1356. mbhc->mbhc_cb->hph_pull_up_control_v2(component,
  1357. HS_PULLUP_I_OFF);
  1358. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1359. mbhc->mbhc_cb->hph_pull_up_control(component, I_OFF);
  1360. else
  1361. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL,
  1362. 0);
  1363. }
  1364. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_PLUG_TYPE, mbhc->hphl_swh);
  1365. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_PLUG_TYPE, mbhc->gnd_swh);
  1366. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_SW_HPH_LP_100K_TO_GND, 1);
  1367. if (mbhc->mbhc_cfg->gnd_det_en && mbhc->mbhc_cb->mbhc_gnd_det_ctrl)
  1368. mbhc->mbhc_cb->mbhc_gnd_det_ctrl(component, true);
  1369. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL, 1);
  1370. /*
  1371. * Disable L_DET for USB-C analog audio to avoid spurious interrupts
  1372. * when a non-audio accessory is inserted. L_DET_EN sets to 1 when FSA
  1373. * I2C driver notifies that ANALOG_AUDIO_ADAPTER is inserted
  1374. */
  1375. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1376. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  1377. else
  1378. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1379. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1380. /* Insertion debounce set to 48ms */
  1381. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 4);
  1382. } else {
  1383. /* Insertion debounce set to 96ms */
  1384. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 6);
  1385. }
  1386. /* Button Debounce set to 16ms */
  1387. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_DBNC, 2);
  1388. /* enable bias */
  1389. mbhc->mbhc_cb->mbhc_bias(component, true);
  1390. /* enable MBHC clock */
  1391. if (mbhc->mbhc_cb->clk_setup) {
  1392. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1393. mbhc->mbhc_cb->clk_setup(component, false);
  1394. else
  1395. mbhc->mbhc_cb->clk_setup(component, true);
  1396. }
  1397. /* program HS_VREF value */
  1398. wcd_program_hs_vref(mbhc);
  1399. wcd_program_btn_threshold(mbhc, false);
  1400. reinit_completion(&mbhc->btn_press_compl);
  1401. WCD_MBHC_RSC_UNLOCK(mbhc);
  1402. pr_debug("%s: leave\n", __func__);
  1403. return ret;
  1404. }
  1405. static void wcd_mbhc_fw_read(struct work_struct *work)
  1406. {
  1407. struct delayed_work *dwork;
  1408. struct wcd_mbhc *mbhc;
  1409. struct snd_soc_component *component;
  1410. const struct firmware *fw;
  1411. struct firmware_cal *fw_data = NULL;
  1412. int ret = -1, retry = 0;
  1413. bool use_default_cal = false;
  1414. dwork = to_delayed_work(work);
  1415. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_firmware_dwork);
  1416. component = mbhc->component;
  1417. while (retry < FW_READ_ATTEMPTS) {
  1418. retry++;
  1419. pr_debug("%s:Attempt %d to request MBHC firmware\n",
  1420. __func__, retry);
  1421. if (mbhc->mbhc_cb->get_hwdep_fw_cal)
  1422. fw_data = mbhc->mbhc_cb->get_hwdep_fw_cal(mbhc,
  1423. WCD9XXX_MBHC_CAL);
  1424. if (!fw_data)
  1425. ret = request_firmware(&fw, "wcd9320/wcd9320_mbhc.bin",
  1426. component->dev);
  1427. /*
  1428. * if request_firmware and hwdep cal both fail then
  1429. * sleep for 4sec for the userspace to send data to kernel
  1430. * retry for few times before bailing out
  1431. */
  1432. if ((ret != 0) && !fw_data) {
  1433. usleep_range(FW_READ_TIMEOUT, FW_READ_TIMEOUT +
  1434. WCD_MBHC_USLEEP_RANGE_MARGIN_US);
  1435. } else {
  1436. pr_debug("%s: MBHC Firmware read successful\n",
  1437. __func__);
  1438. break;
  1439. }
  1440. }
  1441. if (!fw_data)
  1442. pr_debug("%s: using request_firmware\n", __func__);
  1443. else
  1444. pr_debug("%s: using hwdep cal\n", __func__);
  1445. if (ret != 0 && !fw_data) {
  1446. pr_err("%s: Cannot load MBHC firmware use default cal\n",
  1447. __func__);
  1448. use_default_cal = true;
  1449. }
  1450. if (!use_default_cal) {
  1451. const void *data;
  1452. size_t size;
  1453. if (fw_data) {
  1454. data = fw_data->data;
  1455. size = fw_data->size;
  1456. } else {
  1457. data = fw->data;
  1458. size = fw->size;
  1459. }
  1460. if (wcd_mbhc_fw_validate(data, size) == false) {
  1461. pr_err("%s: Invalid MBHC cal data size use default cal\n",
  1462. __func__);
  1463. if (!fw_data)
  1464. release_firmware(fw);
  1465. } else {
  1466. if (fw_data) {
  1467. mbhc->mbhc_cfg->calibration =
  1468. (void *)fw_data->data;
  1469. mbhc->mbhc_cal = fw_data;
  1470. } else {
  1471. mbhc->mbhc_cfg->calibration =
  1472. (void *)fw->data;
  1473. mbhc->mbhc_fw = fw;
  1474. }
  1475. }
  1476. }
  1477. (void) wcd_mbhc_initialise(mbhc);
  1478. }
  1479. static int wcd_mbhc_set_keycode(struct wcd_mbhc *mbhc)
  1480. {
  1481. enum snd_jack_types type;
  1482. int i, ret, result = 0;
  1483. int *btn_key_code;
  1484. btn_key_code = mbhc->mbhc_cfg->key_code;
  1485. for (i = 0 ; i < WCD_MBHC_KEYCODE_NUM ; i++) {
  1486. if (btn_key_code[i] != 0) {
  1487. switch (i) {
  1488. case 0:
  1489. type = SND_JACK_BTN_0;
  1490. break;
  1491. case 1:
  1492. type = SND_JACK_BTN_1;
  1493. break;
  1494. case 2:
  1495. type = SND_JACK_BTN_2;
  1496. break;
  1497. case 3:
  1498. type = SND_JACK_BTN_3;
  1499. break;
  1500. case 4:
  1501. type = SND_JACK_BTN_4;
  1502. break;
  1503. case 5:
  1504. type = SND_JACK_BTN_5;
  1505. break;
  1506. default:
  1507. WARN_ONCE(1, "Wrong button number:%d\n", i);
  1508. result = -1;
  1509. return result;
  1510. }
  1511. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1512. type,
  1513. btn_key_code[i]);
  1514. if (ret) {
  1515. pr_err("%s: Failed to set code for %d\n",
  1516. __func__, btn_key_code[i]);
  1517. result = -1;
  1518. return result;
  1519. }
  1520. input_set_capability(
  1521. mbhc->button_jack.jack->input_dev,
  1522. EV_KEY, btn_key_code[i]);
  1523. pr_debug("%s: set btn%d key code:%d\n", __func__,
  1524. i, btn_key_code[i]);
  1525. }
  1526. }
  1527. if (btn_key_code[0])
  1528. mbhc->is_btn_already_regd = true;
  1529. return result;
  1530. }
  1531. #if IS_ENABLED(CONFIG_QCOM_FSA4480_I2C) || IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1532. static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
  1533. unsigned long mode, void *ptr)
  1534. {
  1535. struct wcd_mbhc *mbhc = container_of(nb, struct wcd_mbhc, aatc_dev_nb);
  1536. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1537. int l_det_en = 0, detection_type = 0;
  1538. bool *cable_status = (bool*) ptr;
  1539. #endif
  1540. if (!mbhc)
  1541. return -EINVAL;
  1542. if (mode == TYPEC_ACCESSORY_AUDIO) {
  1543. dev_dbg(mbhc->component->dev, "enter, %s: mode = %lu\n", __func__, mode);
  1544. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1545. if (cable_status == NULL)
  1546. wcd_usbss_switch_update(WCD_USBSS_AATC, WCD_USBSS_CABLE_CONNECT);
  1547. else {
  1548. if (*cable_status == false)
  1549. wcd_usbss_switch_update(WCD_USBSS_AATC, WCD_USBSS_CABLE_CONNECT);
  1550. else
  1551. dev_dbg(mbhc->component->dev, "skip AATC switch settings, cable_status= %d",
  1552. *cable_status);
  1553. }
  1554. #endif
  1555. if (mbhc->mbhc_cb->clk_setup)
  1556. mbhc->mbhc_cb->clk_setup(mbhc->component, true);
  1557. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1558. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1559. if (unlikely((mbhc->mbhc_cb->lock_sleep(mbhc, true)) == false))
  1560. pr_warn("%s: failed to hold suspend\n", __func__);
  1561. else {
  1562. if (mbhc->current_plug == MBHC_PLUG_TYPE_NONE)
  1563. wcd_mbhc_swch_irq_handler(mbhc);
  1564. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1565. }
  1566. #endif
  1567. } else if (mode < TYPEC_MAX_ACCESSORY) {
  1568. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1569. WCD_MBHC_REG_READ(WCD_MBHC_L_DET_EN, l_det_en);
  1570. WCD_MBHC_REG_READ(WCD_MBHC_MECH_DETECTION_TYPE, detection_type);
  1571. if ((mode == TYPEC_ACCESSORY_NONE) && !detection_type) {
  1572. wcd_usbss_switch_update(WCD_USBSS_AATC, WCD_USBSS_CABLE_DISCONNECT);
  1573. if (unlikely((mbhc->mbhc_cb->lock_sleep(mbhc, true)) == false))
  1574. pr_warn("%s: failed to hold suspend\n", __func__);
  1575. else {
  1576. wcd_mbhc_swch_irq_handler(mbhc);
  1577. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1578. }
  1579. dev_dbg(mbhc->component->dev, "leave, %s: mode = %lu\n", __func__, mode);
  1580. }
  1581. #endif
  1582. } else if (mode == TYPEC_MAX_ACCESSORY) {
  1583. if (mbhc->mbhc_cb->surge_reset_routine)
  1584. mbhc->mbhc_cb->surge_reset_routine(mbhc);
  1585. }
  1586. return 0;
  1587. }
  1588. #else
  1589. static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
  1590. unsigned long mode, void *ptr)
  1591. {
  1592. pr_info("%s: mode = %lu, handler not implemented\n", __func__, mode);
  1593. return 0;
  1594. }
  1595. #endif
  1596. int wcd_mbhc_start(struct wcd_mbhc *mbhc, struct wcd_mbhc_config *mbhc_cfg)
  1597. {
  1598. int rc = 0;
  1599. struct snd_soc_component *component;
  1600. struct snd_soc_card *card;
  1601. const char *usb_c_dt = "qcom,msm-mbhc-usbc-audio-supported";
  1602. if (!mbhc || !mbhc_cfg)
  1603. return -EINVAL;
  1604. component = mbhc->component;
  1605. card = component->card;
  1606. /* update the mbhc config */
  1607. mbhc->mbhc_cfg = mbhc_cfg;
  1608. dev_dbg(mbhc->component->dev, "%s: enter\n", __func__);
  1609. /* check if USB C analog is defined on device tree */
  1610. mbhc_cfg->enable_usbc_analog = 0;
  1611. if (of_find_property(card->dev->of_node, usb_c_dt, NULL)) {
  1612. rc = of_property_read_u32(card->dev->of_node, usb_c_dt,
  1613. &mbhc_cfg->enable_usbc_analog);
  1614. }
  1615. if (mbhc_cfg->enable_usbc_analog == 0 || rc != 0) {
  1616. dev_dbg(card->dev,
  1617. "%s: %s in dt node is missing or false\n",
  1618. __func__, usb_c_dt);
  1619. dev_dbg(card->dev,
  1620. "%s: skipping USB c analog configuration\n", __func__);
  1621. }
  1622. /* Parse wcd_usbss/fsa switch handle */
  1623. if (mbhc_cfg->enable_usbc_analog) {
  1624. dev_dbg(mbhc->component->dev, "%s: usbc analog enabled\n",
  1625. __func__);
  1626. mbhc->swap_thr = GND_MIC_USBC_SWAP_THRESHOLD;
  1627. if (IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C))
  1628. mbhc->aatc_dev_np = of_parse_phandle(card->dev->of_node,
  1629. "wcd939x-i2c-handle", 0);
  1630. else if (IS_ENABLED(CONFIG_QCOM_FSA4480_I2C))
  1631. mbhc->aatc_dev_np = of_parse_phandle(card->dev->of_node,
  1632. "fsa4480-i2c-handle", 0);
  1633. if (!mbhc->aatc_dev_np) {
  1634. dev_err(card->dev, "%s: wcd939x or fsa i2c node not found\n",
  1635. __func__);
  1636. rc = -EINVAL;
  1637. goto err;
  1638. }
  1639. }
  1640. /* Disable moisture detect and duty cycle for WCD USB AATC HS*/
  1641. if (mbhc_cfg->enable_usbc_analog) {
  1642. mbhc_cfg->moisture_en = false;
  1643. mbhc_cfg->moisture_duty_cycle_en = false;
  1644. pr_debug("%s: Disable moisture detect and duty cycle of AATC",
  1645. __func__);
  1646. }
  1647. /* Set btn key code */
  1648. if ((!mbhc->is_btn_already_regd) && wcd_mbhc_set_keycode(mbhc))
  1649. pr_err("Set btn key code error!!!\n");
  1650. if (!mbhc->mbhc_cfg->read_fw_bin ||
  1651. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_fw) ||
  1652. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_cal)) {
  1653. rc = wcd_mbhc_initialise(mbhc);
  1654. if (rc) {
  1655. dev_err(card->dev, "%s: wcd mbhc initialize failed\n",
  1656. __func__);
  1657. goto err;
  1658. }
  1659. } else {
  1660. if (!mbhc->mbhc_fw || !mbhc->mbhc_cal)
  1661. schedule_delayed_work(&mbhc->mbhc_firmware_dwork,
  1662. usecs_to_jiffies(FW_READ_TIMEOUT));
  1663. else
  1664. pr_err("%s: Skipping to read mbhc fw, 0x%pK %pK\n",
  1665. __func__, mbhc->mbhc_fw, mbhc->mbhc_cal);
  1666. }
  1667. if (mbhc_cfg->enable_usbc_analog) {
  1668. mbhc->aatc_dev_nb.notifier_call = wcd_mbhc_usbc_ana_event_handler;
  1669. mbhc->aatc_dev_nb.priority = 0;
  1670. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1671. rc = wcd_usbss_reg_notifier(&mbhc->aatc_dev_nb, mbhc->aatc_dev_np);
  1672. #endif
  1673. #if IS_ENABLED(CONFIG_QCOM_FSA4480_I2C)
  1674. rc = fsa4480_reg_notifier(&mbhc->aatc_dev_nb, mbhc->aatc_dev_np);
  1675. #endif
  1676. }
  1677. return rc;
  1678. err:
  1679. dev_dbg(mbhc->component->dev, "%s: leave %d\n", __func__, rc);
  1680. return rc;
  1681. }
  1682. EXPORT_SYMBOL(wcd_mbhc_start);
  1683. void wcd_mbhc_stop(struct wcd_mbhc *mbhc)
  1684. {
  1685. pr_debug("%s: enter\n", __func__);
  1686. if (mbhc->current_plug != MBHC_PLUG_TYPE_NONE) {
  1687. if (mbhc->mbhc_cb && mbhc->mbhc_cb->skip_imped_detect)
  1688. mbhc->mbhc_cb->skip_imped_detect(mbhc->component);
  1689. }
  1690. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1691. mbhc->hph_status = 0;
  1692. if (mbhc->mbhc_cb && mbhc->mbhc_cb->irq_control) {
  1693. mbhc->mbhc_cb->irq_control(mbhc->component,
  1694. mbhc->intr_ids->hph_left_ocp,
  1695. false);
  1696. mbhc->mbhc_cb->irq_control(mbhc->component,
  1697. mbhc->intr_ids->hph_right_ocp,
  1698. false);
  1699. }
  1700. if (mbhc->mbhc_fw || mbhc->mbhc_cal) {
  1701. cancel_delayed_work_sync(&mbhc->mbhc_firmware_dwork);
  1702. if (!mbhc->mbhc_cal)
  1703. release_firmware(mbhc->mbhc_fw);
  1704. mbhc->mbhc_fw = NULL;
  1705. mbhc->mbhc_cal = NULL;
  1706. }
  1707. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1708. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1709. wcd_usbss_unreg_notifier(&mbhc->aatc_dev_nb, mbhc->aatc_dev_np);
  1710. #endif
  1711. #if IS_ENABLED(CONFIG_QCOM_FSA4480_I2C)
  1712. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1713. fsa4480_unreg_notifier(&mbhc->aatc_dev_nb, mbhc->aatc_dev_np);
  1714. #endif
  1715. pr_debug("%s: leave\n", __func__);
  1716. }
  1717. EXPORT_SYMBOL(wcd_mbhc_stop);
  1718. /*
  1719. * wcd_mbhc_init : initialize MBHC internal structures.
  1720. *
  1721. * NOTE: mbhc->mbhc_cfg is not YET configure so shouldn't be used
  1722. */
  1723. int wcd_mbhc_init(struct wcd_mbhc *mbhc, struct snd_soc_component *component,
  1724. const struct wcd_mbhc_cb *mbhc_cb,
  1725. const struct wcd_mbhc_intr *mbhc_cdc_intr_ids,
  1726. struct wcd_mbhc_register *wcd_mbhc_regs,
  1727. bool impedance_det_en)
  1728. {
  1729. int ret = 0;
  1730. int hph_swh = 0;
  1731. int gnd_swh = 0;
  1732. u32 hph_moist_config[3];
  1733. struct snd_soc_card *card = component->card;
  1734. const char *hph_switch = "qcom,msm-mbhc-hphl-swh";
  1735. const char *gnd_switch = "qcom,msm-mbhc-gnd-swh";
  1736. const char *hs_thre = "qcom,msm-mbhc-hs-mic-max-threshold-mv";
  1737. const char *hph_thre = "qcom,msm-mbhc-hs-mic-min-threshold-mv";
  1738. pr_debug("%s: enter\n", __func__);
  1739. ret = of_property_read_u32(card->dev->of_node, hph_switch, &hph_swh);
  1740. if (ret) {
  1741. dev_err(card->dev,
  1742. "%s: missing %s in dt node\n", __func__, hph_switch);
  1743. goto err;
  1744. }
  1745. ret = of_property_read_u32(card->dev->of_node, gnd_switch, &gnd_swh);
  1746. if (ret) {
  1747. dev_err(card->dev,
  1748. "%s: missing %s in dt node\n", __func__, gnd_switch);
  1749. goto err;
  1750. }
  1751. ret = of_property_read_u32(card->dev->of_node, hs_thre,
  1752. &(mbhc->hs_thr));
  1753. if (ret)
  1754. dev_dbg(card->dev,
  1755. "%s: missing %s in dt node\n", __func__, hs_thre);
  1756. ret = of_property_read_u32(card->dev->of_node, hph_thre,
  1757. &(mbhc->hph_thr));
  1758. if (ret)
  1759. dev_dbg(card->dev,
  1760. "%s: missing %s in dt node\n", __func__, hph_thre);
  1761. ret = of_property_read_u32_array(card->dev->of_node,
  1762. "qcom,msm-mbhc-moist-cfg",
  1763. hph_moist_config, 3);
  1764. if (ret) {
  1765. dev_dbg(card->dev, "%s: no qcom,msm-mbhc-moist-cfg in DT\n",
  1766. __func__);
  1767. mbhc->moist_vref = V_45_MV;
  1768. mbhc->moist_iref = I_3P0_UA;
  1769. mbhc->moist_rref = R_24_KOHM;
  1770. } else {
  1771. mbhc->moist_vref = hph_moist_config[0];
  1772. mbhc->moist_iref = hph_moist_config[1];
  1773. mbhc->moist_rref = hph_moist_config[2];
  1774. }
  1775. mbhc->in_swch_irq_handler = false;
  1776. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1777. mbhc->is_btn_press = false;
  1778. mbhc->component = component;
  1779. mbhc->intr_ids = mbhc_cdc_intr_ids;
  1780. mbhc->impedance_detect = impedance_det_en;
  1781. mbhc->hphl_swh = hph_swh;
  1782. mbhc->gnd_swh = gnd_swh;
  1783. mbhc->micbias_enable = false;
  1784. mbhc->mbhc_cb = mbhc_cb;
  1785. mbhc->btn_press_intr = false;
  1786. mbhc->is_hs_recording = false;
  1787. mbhc->is_extn_cable = false;
  1788. mbhc->extn_cable_hph_rem = false;
  1789. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  1790. mbhc->wcd_mbhc_regs = wcd_mbhc_regs;
  1791. mbhc->swap_thr = GND_MIC_SWAP_THRESHOLD;
  1792. mbhc->hphl_cross_conn_thr = HPHL_CROSS_CONN_THRESHOLD;
  1793. mbhc->hphr_cross_conn_thr = HPHR_CROSS_CONN_THRESHOLD;
  1794. if (mbhc->intr_ids == NULL) {
  1795. pr_err("%s: Interrupt mapping not provided\n", __func__);
  1796. return -EINVAL;
  1797. }
  1798. if (!mbhc->wcd_mbhc_regs) {
  1799. dev_err(component->dev, "%s: mbhc registers are not defined\n",
  1800. __func__);
  1801. return -EINVAL;
  1802. }
  1803. /* Check if IRQ and other required callbacks are defined or not */
  1804. if (!mbhc_cb || !mbhc_cb->request_irq || !mbhc_cb->irq_control ||
  1805. !mbhc_cb->free_irq || !mbhc_cb->map_btn_code_to_num ||
  1806. !mbhc_cb->lock_sleep || !mbhc_cb->mbhc_bias ||
  1807. !mbhc_cb->set_btn_thr) {
  1808. dev_err(component->dev, "%s: required mbhc callbacks are not defined\n",
  1809. __func__);
  1810. return -EINVAL;
  1811. }
  1812. /* No need to create new sound card jacks if is is already created */
  1813. if (mbhc->headset_jack.jack == NULL) {
  1814. ret = snd_soc_card_jack_new(component->card,
  1815. "Headset Jack", WCD_MBHC_JACK_MASK,
  1816. &mbhc->headset_jack);
  1817. if (ret) {
  1818. pr_err("%s: Failed to create new jack\n", __func__);
  1819. return ret;
  1820. }
  1821. ret = snd_soc_card_jack_new(component->card,
  1822. "Button Jack",
  1823. WCD_MBHC_JACK_BUTTON_MASK,
  1824. &mbhc->button_jack);
  1825. if (ret) {
  1826. pr_err("Failed to create new jack\n");
  1827. return ret;
  1828. }
  1829. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1830. SND_JACK_BTN_0,
  1831. KEY_MEDIA);
  1832. if (ret) {
  1833. pr_err("%s: Failed to set code for btn-0\n",
  1834. __func__);
  1835. return ret;
  1836. }
  1837. INIT_DELAYED_WORK(&mbhc->mbhc_firmware_dwork,
  1838. wcd_mbhc_fw_read);
  1839. INIT_DELAYED_WORK(&mbhc->mbhc_btn_dwork, wcd_btn_lpress_fn);
  1840. }
  1841. init_completion(&mbhc->btn_press_compl);
  1842. /* Register event notifier */
  1843. mbhc->nblock.notifier_call = wcd_event_notify;
  1844. if (mbhc->mbhc_cb->register_notifier) {
  1845. ret = mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock,
  1846. true);
  1847. if (ret) {
  1848. pr_err("%s: Failed to register notifier %d\n",
  1849. __func__, ret);
  1850. return ret;
  1851. }
  1852. }
  1853. init_waitqueue_head(&mbhc->wait_btn_press);
  1854. mutex_init(&mbhc->codec_resource_lock);
  1855. switch (mbhc->mbhc_detection_logic) {
  1856. case WCD_DETECTION_LEGACY:
  1857. wcd_mbhc_legacy_init(mbhc);
  1858. break;
  1859. case WCD_DETECTION_ADC:
  1860. wcd_mbhc_adc_init(mbhc);
  1861. break;
  1862. default:
  1863. pr_err("%s: Unknown detection logic type %d\n",
  1864. __func__, mbhc->mbhc_detection_logic);
  1865. break;
  1866. }
  1867. if (!mbhc->mbhc_fn ||
  1868. !mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq ||
  1869. !mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq ||
  1870. !mbhc->mbhc_fn->wcd_mbhc_detect_plug_type ||
  1871. !mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1872. pr_err("%s: mbhc function pointer is NULL\n", __func__);
  1873. goto err_mbhc_sw_irq;
  1874. }
  1875. ret = mbhc->mbhc_cb->request_irq(component,
  1876. mbhc->intr_ids->mbhc_sw_intr,
  1877. wcd_mbhc_mech_plug_detect_irq,
  1878. "mbhc sw intr", mbhc);
  1879. if (ret) {
  1880. pr_err("%s: Failed to request irq %d, ret = %d\n", __func__,
  1881. mbhc->intr_ids->mbhc_sw_intr, ret);
  1882. goto err_mbhc_sw_irq;
  1883. }
  1884. ret = mbhc->mbhc_cb->request_irq(component,
  1885. mbhc->intr_ids->mbhc_btn_press_intr,
  1886. wcd_mbhc_btn_press_handler,
  1887. "Button Press detect", mbhc);
  1888. if (ret) {
  1889. pr_err("%s: Failed to request irq %d\n", __func__,
  1890. mbhc->intr_ids->mbhc_btn_press_intr);
  1891. goto err_btn_press_irq;
  1892. }
  1893. ret = mbhc->mbhc_cb->request_irq(component,
  1894. mbhc->intr_ids->mbhc_btn_release_intr,
  1895. wcd_mbhc_release_handler,
  1896. "Button Release detect", mbhc);
  1897. if (ret) {
  1898. pr_err("%s: Failed to request irq %d\n", __func__,
  1899. mbhc->intr_ids->mbhc_btn_release_intr);
  1900. goto err_btn_release_irq;
  1901. }
  1902. ret = mbhc->mbhc_cb->request_irq(component,
  1903. mbhc->intr_ids->mbhc_hs_ins_intr,
  1904. mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq,
  1905. "Elect Insert", mbhc);
  1906. if (ret) {
  1907. pr_err("%s: Failed to request irq %d\n", __func__,
  1908. mbhc->intr_ids->mbhc_hs_ins_intr);
  1909. goto err_mbhc_hs_ins_irq;
  1910. }
  1911. mbhc->mbhc_cb->irq_control(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1912. false);
  1913. clear_bit(WCD_MBHC_ELEC_HS_INS, &mbhc->intr_status);
  1914. ret = mbhc->mbhc_cb->request_irq(component,
  1915. mbhc->intr_ids->mbhc_hs_rem_intr,
  1916. mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq,
  1917. "Elect Remove", mbhc);
  1918. if (ret) {
  1919. pr_err("%s: Failed to request irq %d\n", __func__,
  1920. mbhc->intr_ids->mbhc_hs_rem_intr);
  1921. goto err_mbhc_hs_rem_irq;
  1922. }
  1923. mbhc->mbhc_cb->irq_control(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  1924. false);
  1925. clear_bit(WCD_MBHC_ELEC_HS_REM, &mbhc->intr_status);
  1926. ret = mbhc->mbhc_cb->request_irq(component,
  1927. mbhc->intr_ids->hph_left_ocp,
  1928. wcd_mbhc_hphl_ocp_irq, "HPH_L OCP detect",
  1929. mbhc);
  1930. if (ret) {
  1931. pr_err("%s: Failed to request irq %d\n", __func__,
  1932. mbhc->intr_ids->hph_left_ocp);
  1933. goto err_hphl_ocp_irq;
  1934. }
  1935. ret = mbhc->mbhc_cb->request_irq(component,
  1936. mbhc->intr_ids->hph_right_ocp,
  1937. wcd_mbhc_hphr_ocp_irq, "HPH_R OCP detect",
  1938. mbhc);
  1939. if (ret) {
  1940. pr_err("%s: Failed to request irq %d\n", __func__,
  1941. mbhc->intr_ids->hph_right_ocp);
  1942. goto err_hphr_ocp_irq;
  1943. }
  1944. if (!mbhc->extdev) {
  1945. mbhc->extdev =
  1946. devm_extcon_dev_allocate(component->dev,
  1947. mbhc_ext_dev_supported_table);
  1948. if (IS_ERR(mbhc->extdev)) {
  1949. goto err_ext_dev;
  1950. ret = PTR_ERR(mbhc->extdev);
  1951. }
  1952. ret = devm_extcon_dev_register(component->dev, mbhc->extdev);
  1953. if (ret) {
  1954. pr_err("%s:audio registration failed\n", __func__);
  1955. goto err_ext_dev;
  1956. }
  1957. }
  1958. mbhc->deinit_in_progress = false;
  1959. pr_debug("%s: leave ret %d\n", __func__, ret);
  1960. return ret;
  1961. err_ext_dev:
  1962. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_right_ocp, mbhc);
  1963. err_hphr_ocp_irq:
  1964. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_left_ocp, mbhc);
  1965. err_hphl_ocp_irq:
  1966. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  1967. mbhc);
  1968. err_mbhc_hs_rem_irq:
  1969. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1970. mbhc);
  1971. err_mbhc_hs_ins_irq:
  1972. mbhc->mbhc_cb->free_irq(component,
  1973. mbhc->intr_ids->mbhc_btn_release_intr,
  1974. mbhc);
  1975. err_btn_release_irq:
  1976. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_btn_press_intr,
  1977. mbhc);
  1978. err_btn_press_irq:
  1979. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1980. err_mbhc_sw_irq:
  1981. if (mbhc->mbhc_cb->register_notifier)
  1982. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1983. mutex_destroy(&mbhc->codec_resource_lock);
  1984. err:
  1985. pr_debug("%s: leave ret %d\n", __func__, ret);
  1986. return ret;
  1987. }
  1988. EXPORT_SYMBOL(wcd_mbhc_init);
  1989. void wcd_mbhc_deinit(struct wcd_mbhc *mbhc)
  1990. {
  1991. struct snd_soc_component *component = mbhc->component;
  1992. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1993. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_btn_press_intr,
  1994. mbhc);
  1995. mbhc->mbhc_cb->free_irq(component,
  1996. mbhc->intr_ids->mbhc_btn_release_intr,
  1997. mbhc);
  1998. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1999. mbhc);
  2000. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  2001. mbhc);
  2002. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_left_ocp, mbhc);
  2003. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_right_ocp, mbhc);
  2004. if (mbhc->mbhc_cb && mbhc->mbhc_cb->register_notifier)
  2005. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  2006. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  2007. WCD_MBHC_RSC_LOCK(mbhc);
  2008. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  2009. &mbhc->correct_plug_swch);
  2010. WCD_MBHC_RSC_UNLOCK(mbhc);
  2011. }
  2012. mutex_destroy(&mbhc->codec_resource_lock);
  2013. }
  2014. EXPORT_SYMBOL(wcd_mbhc_deinit);
  2015. static int __init mbhc_init(void)
  2016. {
  2017. mutex_init(&hphl_pa_lock);
  2018. mutex_init(&hphr_pa_lock);
  2019. return 0;
  2020. }
  2021. static void __exit mbhc_exit(void)
  2022. {
  2023. mutex_destroy(&hphl_pa_lock);
  2024. mutex_destroy(&hphr_pa_lock);
  2025. }
  2026. module_init(mbhc_init);
  2027. module_exit(mbhc_exit);
  2028. MODULE_DESCRIPTION("wcd MBHC v2 module");
  2029. MODULE_LICENSE("GPL v2");