wcd-mbhc-v2.c 61 KB

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