wcd-mbhc-v2.c 60 KB

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