wcd-mbhc-v2.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  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. *zl = mbhc->zl;
  475. *zr = mbhc->zr;
  476. if (*zl && *zr)
  477. return 0;
  478. else
  479. return -EINVAL;
  480. }
  481. EXPORT_SYMBOL(wcd_mbhc_get_impedance);
  482. void wcd_mbhc_hs_elec_irq(struct wcd_mbhc *mbhc, int irq_type,
  483. bool enable)
  484. {
  485. int irq;
  486. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  487. if (irq_type == WCD_MBHC_ELEC_HS_INS)
  488. irq = mbhc->intr_ids->mbhc_hs_ins_intr;
  489. else if (irq_type == WCD_MBHC_ELEC_HS_REM)
  490. irq = mbhc->intr_ids->mbhc_hs_rem_intr;
  491. else {
  492. pr_debug("%s: irq_type: %d, enable: %d\n",
  493. __func__, irq_type, enable);
  494. return;
  495. }
  496. pr_debug("%s: irq: %d, enable: %d, intr_status:%lu\n",
  497. __func__, irq, enable, mbhc->intr_status);
  498. if ((test_bit(irq_type, &mbhc->intr_status)) != enable) {
  499. mbhc->mbhc_cb->irq_control(mbhc->component, irq, enable);
  500. if (enable)
  501. set_bit(irq_type, &mbhc->intr_status);
  502. else
  503. clear_bit(irq_type, &mbhc->intr_status);
  504. }
  505. }
  506. EXPORT_SYMBOL(wcd_mbhc_hs_elec_irq);
  507. void wcd_mbhc_report_plug(struct wcd_mbhc *mbhc, int insertion,
  508. enum snd_jack_types jack_type)
  509. {
  510. struct snd_soc_component *component = mbhc->component;
  511. bool is_pa_on = false;
  512. u8 fsm_en = 0;
  513. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  514. pr_debug("%s: enter insertion %d hph_status %x\n",
  515. __func__, insertion, mbhc->hph_status);
  516. if (!insertion) {
  517. /* Report removal */
  518. mbhc->hph_status &= ~jack_type;
  519. /*
  520. * cancel possibly scheduled btn work and
  521. * report release if we reported button press
  522. */
  523. if (wcd_cancel_btn_work(mbhc)) {
  524. pr_debug("%s: button press is canceled\n", __func__);
  525. } else if (mbhc->buttons_pressed) {
  526. pr_debug("%s: release of button press%d\n",
  527. __func__, jack_type);
  528. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack, 0,
  529. mbhc->buttons_pressed);
  530. mbhc->buttons_pressed &=
  531. ~WCD_MBHC_JACK_BUTTON_MASK;
  532. }
  533. if (mbhc->micbias_enable) {
  534. if (mbhc->mbhc_cb->mbhc_micbias_control)
  535. mbhc->mbhc_cb->mbhc_micbias_control(
  536. component, MIC_BIAS_2,
  537. MICB_DISABLE);
  538. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  539. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  540. component,
  541. MIC_BIAS_2, false);
  542. if (mbhc->mbhc_cb->set_micbias_value) {
  543. mbhc->mbhc_cb->set_micbias_value(component);
  544. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MICB_CTRL, 0);
  545. }
  546. mbhc->micbias_enable = false;
  547. }
  548. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  549. mbhc->zl = mbhc->zr = 0;
  550. pr_debug("%s: Reporting removal %d(%x)\n", __func__,
  551. jack_type, mbhc->hph_status);
  552. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  553. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  554. wcd_mbhc_set_and_turnoff_hph_padac(mbhc);
  555. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  556. hphrocp_off_report(mbhc, SND_JACK_OC_HPHR);
  557. hphlocp_off_report(mbhc, SND_JACK_OC_HPHL);
  558. #endif /* CONFIG_AUDIO_QGKI */
  559. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  560. mbhc->force_linein = false;
  561. } else {
  562. /*
  563. * Report removal of current jack type.
  564. * Headphone to headset shouldn't report headphone
  565. * removal.
  566. */
  567. if (mbhc->mbhc_cfg->detect_extn_cable &&
  568. (mbhc->current_plug == MBHC_PLUG_TYPE_HIGH_HPH ||
  569. jack_type == SND_JACK_LINEOUT) &&
  570. (mbhc->hph_status && mbhc->hph_status != jack_type)) {
  571. if (mbhc->micbias_enable &&
  572. mbhc->hph_status == SND_JACK_HEADSET) {
  573. if (mbhc->mbhc_cb->mbhc_micbias_control)
  574. mbhc->mbhc_cb->mbhc_micbias_control(
  575. component, MIC_BIAS_2,
  576. MICB_DISABLE);
  577. if (mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic)
  578. mbhc->mbhc_cb->mbhc_micb_ctrl_thr_mic(
  579. component,
  580. MIC_BIAS_2, false);
  581. if (mbhc->mbhc_cb->set_micbias_value) {
  582. mbhc->mbhc_cb->set_micbias_value(
  583. component);
  584. WCD_MBHC_REG_UPDATE_BITS(
  585. WCD_MBHC_MICB_CTRL, 0);
  586. }
  587. mbhc->micbias_enable = false;
  588. }
  589. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  590. mbhc->zl = mbhc->zr = 0;
  591. if (!mbhc->force_linein) {
  592. pr_debug("%s: Reporting removal (%x)\n",
  593. __func__, mbhc->hph_status);
  594. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  595. 0, WCD_MBHC_JACK_MASK);
  596. }
  597. if (mbhc->hph_status == SND_JACK_LINEOUT) {
  598. pr_debug("%s: Enable micbias\n", __func__);
  599. /* Disable current source and enable micbias */
  600. wcd_enable_curr_micbias(mbhc, WCD_MBHC_EN_MB);
  601. pr_debug("%s: set up elec removal detection\n",
  602. __func__);
  603. usleep_range(200, 210);
  604. wcd_mbhc_hs_elec_irq(mbhc,
  605. WCD_MBHC_ELEC_HS_REM,
  606. true);
  607. }
  608. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  609. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  610. SND_JACK_LINEOUT |
  611. SND_JACK_UNSUPPORTED);
  612. #else
  613. mbhc->hph_status &= ~(SND_JACK_HEADSET |
  614. SND_JACK_LINEOUT);
  615. #endif /* CONFIG_AUDIO_QGKI */
  616. }
  617. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET &&
  618. jack_type == SND_JACK_HEADPHONE)
  619. mbhc->hph_status &= ~SND_JACK_HEADSET;
  620. /* Report insertion */
  621. if (jack_type == SND_JACK_HEADPHONE)
  622. mbhc->current_plug = MBHC_PLUG_TYPE_HEADPHONE;
  623. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  624. else if (jack_type == SND_JACK_UNSUPPORTED)
  625. mbhc->current_plug = MBHC_PLUG_TYPE_GND_MIC_SWAP;
  626. #endif /* CONFIG_AUDIO_QGKI */
  627. else if (jack_type == SND_JACK_HEADSET) {
  628. mbhc->current_plug = MBHC_PLUG_TYPE_HEADSET;
  629. mbhc->jiffies_atreport = jiffies;
  630. } else if (jack_type == SND_JACK_LINEOUT)
  631. mbhc->current_plug = MBHC_PLUG_TYPE_HIGH_HPH;
  632. else {
  633. pr_debug("%s: invalid Jack type %d\n",__func__, jack_type);
  634. }
  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. /*
  785. * If Headphone was reported previously, this will
  786. * only report the mic line
  787. */
  788. wcd_mbhc_report_plug(mbhc, 1, jack_type);
  789. } else if (plug_type == MBHC_PLUG_TYPE_HIGH_HPH) {
  790. if (mbhc->mbhc_cfg->detect_extn_cable) {
  791. /* High impedance device found. Report as LINEOUT */
  792. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  793. pr_debug("%s: setup mic trigger for further detection\n",
  794. __func__);
  795. /* Disable HW FSM and current source */
  796. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  797. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  798. /* Setup for insertion detection */
  799. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE,
  800. 1);
  801. /*
  802. * Enable HPHL trigger and MIC Schmitt triggers
  803. * and request for elec insertion interrupts
  804. */
  805. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC,
  806. 3);
  807. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  808. true);
  809. } else {
  810. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  811. }
  812. } else {
  813. WARN(1, "Unexpected current plug_type %d, plug_type %d\n",
  814. mbhc->current_plug, plug_type);
  815. }
  816. exit:
  817. pr_debug("%s: leave\n", __func__);
  818. }
  819. EXPORT_SYMBOL(wcd_mbhc_find_plug_and_report);
  820. static bool wcd_mbhc_moisture_detect(struct wcd_mbhc *mbhc, bool detection_type)
  821. {
  822. bool ret = false;
  823. if (!mbhc->mbhc_cfg->moisture_en &&
  824. !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  825. return ret;
  826. if (!mbhc->mbhc_cb->mbhc_get_moisture_status ||
  827. !mbhc->mbhc_cb->mbhc_moisture_polling_ctrl ||
  828. !mbhc->mbhc_cb->mbhc_moisture_detect_en)
  829. return ret;
  830. if (mbhc->mbhc_cb->mbhc_get_moisture_status(mbhc)) {
  831. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  832. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_DET_EN, 0);
  833. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc, true);
  834. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  835. detection_type);
  836. ret = true;
  837. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  838. if (mbhc->mbhc_cfg->gnd_det_en)
  839. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_DET_EN, 1);
  840. } else {
  841. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc, false);
  842. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc, false);
  843. }
  844. return ret;
  845. }
  846. static void wcd_mbhc_swch_irq_handler(struct wcd_mbhc *mbhc)
  847. {
  848. bool detection_type = 0;
  849. bool micbias1 = false;
  850. struct snd_soc_component *component = mbhc->component;
  851. enum snd_jack_types jack_type;
  852. dev_dbg(component->dev, "%s: enter\n", __func__);
  853. WCD_MBHC_RSC_LOCK(mbhc);
  854. mbhc->in_swch_irq_handler = true;
  855. /* cancel pending button press */
  856. if (wcd_cancel_btn_work(mbhc))
  857. pr_debug("%s: button press is canceled\n", __func__);
  858. WCD_MBHC_REG_READ(WCD_MBHC_MECH_DETECTION_TYPE, detection_type);
  859. /* Set the detection type appropriately */
  860. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  861. !detection_type);
  862. pr_debug("%s: mbhc->current_plug: %d detection_type: %d\n", __func__,
  863. mbhc->current_plug, detection_type);
  864. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug)
  865. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  866. &mbhc->correct_plug_swch);
  867. else
  868. pr_info("%s: hs_detect_plug work not cancelled\n", __func__);
  869. /* Enable micbias ramp */
  870. if (mbhc->mbhc_cb->mbhc_micb_ramp_control)
  871. mbhc->mbhc_cb->mbhc_micb_ramp_control(component, true);
  872. if (mbhc->mbhc_cb->micbias_enable_status)
  873. micbias1 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  874. MIC_BIAS_1);
  875. if ((mbhc->current_plug == MBHC_PLUG_TYPE_NONE) &&
  876. detection_type) {
  877. /* If moisture is present, then enable polling, disable
  878. * moisture detection and wait for interrupt
  879. */
  880. if (wcd_mbhc_moisture_detect(mbhc, detection_type))
  881. goto done;
  882. /* Make sure MASTER_BIAS_CTL is enabled */
  883. mbhc->mbhc_cb->mbhc_bias(component, true);
  884. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  885. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  886. MBHC_COMMON_MICB_TAIL_CURR, true);
  887. if (!mbhc->mbhc_cfg->hs_ext_micbias &&
  888. mbhc->mbhc_cb->micb_internal)
  889. /*
  890. * Enable Tx2 RBias if the headset
  891. * is using internal micbias
  892. */
  893. mbhc->mbhc_cb->micb_internal(component, 1, true);
  894. /* Remove micbias pulldown */
  895. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 0);
  896. /* Apply trim if needed on the device */
  897. if (mbhc->mbhc_cb->trim_btn_reg)
  898. mbhc->mbhc_cb->trim_btn_reg(component);
  899. /* Enable external voltage source to micbias if present */
  900. if (mbhc->mbhc_cb->enable_mb_source)
  901. mbhc->mbhc_cb->enable_mb_source(mbhc, true);
  902. mbhc->btn_press_intr = false;
  903. mbhc->is_btn_press = false;
  904. if (mbhc->mbhc_fn)
  905. mbhc->mbhc_fn->wcd_mbhc_detect_plug_type(mbhc);
  906. } else if ((mbhc->current_plug != MBHC_PLUG_TYPE_NONE)
  907. && !detection_type) {
  908. /* Disable external voltage source to micbias if present */
  909. if (mbhc->mbhc_cb->enable_mb_source)
  910. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  911. /* Disable HW FSM */
  912. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  913. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  914. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  915. mbhc->mbhc_cb->mbhc_common_micb_ctrl(component,
  916. MBHC_COMMON_MICB_TAIL_CURR, false);
  917. if (mbhc->mbhc_cb->set_cap_mode)
  918. mbhc->mbhc_cb->set_cap_mode(component, micbias1, false);
  919. mbhc->btn_press_intr = false;
  920. mbhc->is_btn_press = false;
  921. switch (mbhc->current_plug) {
  922. case MBHC_PLUG_TYPE_HEADPHONE:
  923. jack_type = SND_JACK_HEADPHONE;
  924. break;
  925. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  926. case MBHC_PLUG_TYPE_GND_MIC_SWAP:
  927. jack_type = SND_JACK_UNSUPPORTED;
  928. break;
  929. #endif /* CONFIG_AUDIO_QGKI */
  930. case MBHC_PLUG_TYPE_HEADSET:
  931. /* make sure to turn off Rbias */
  932. if (mbhc->mbhc_cb->micb_internal)
  933. mbhc->mbhc_cb->micb_internal(component,
  934. 1, false);
  935. /* Pulldown micbias */
  936. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 1);
  937. jack_type = SND_JACK_HEADSET;
  938. break;
  939. case MBHC_PLUG_TYPE_HIGH_HPH:
  940. if (mbhc->mbhc_detection_logic == WCD_DETECTION_ADC)
  941. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  942. mbhc->is_extn_cable = false;
  943. jack_type = SND_JACK_LINEOUT;
  944. break;
  945. default:
  946. pr_info("%s: Invalid current plug: %d\n",
  947. __func__, mbhc->current_plug);
  948. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  949. jack_type = SND_JACK_UNSUPPORTED;
  950. #else
  951. jack_type = SND_JACK_HEADPHONE;
  952. #endif /* CONFIG_AUDIO_QGKI */
  953. break;
  954. }
  955. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM, false);
  956. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  957. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE, 1);
  958. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  959. mbhc->extn_cable_hph_rem = false;
  960. wcd_mbhc_report_plug(mbhc, 0, jack_type);
  961. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  962. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  963. if (mbhc->mbhc_cb->clk_setup)
  964. mbhc->mbhc_cb->clk_setup(
  965. mbhc->component, false);
  966. }
  967. if (mbhc->mbhc_cfg->moisture_en ||
  968. mbhc->mbhc_cfg->moisture_duty_cycle_en) {
  969. if (mbhc->mbhc_cb->mbhc_moisture_polling_ctrl)
  970. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc,
  971. false);
  972. if (mbhc->mbhc_cb->mbhc_moisture_detect_en)
  973. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc,
  974. false);
  975. }
  976. } else if (!detection_type) {
  977. /* Disable external voltage source to micbias if present */
  978. if (mbhc->mbhc_cb->enable_mb_source)
  979. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  980. /* Disable HW FSM */
  981. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  982. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  983. mbhc->extn_cable_hph_rem = false;
  984. }
  985. done:
  986. mbhc->in_swch_irq_handler = false;
  987. WCD_MBHC_RSC_UNLOCK(mbhc);
  988. pr_debug("%s: leave\n", __func__);
  989. }
  990. static irqreturn_t wcd_mbhc_mech_plug_detect_irq(int irq, void *data)
  991. {
  992. int r = IRQ_HANDLED;
  993. struct wcd_mbhc *mbhc = data;
  994. pr_debug("%s: enter\n", __func__);
  995. if (mbhc == NULL) {
  996. pr_err("%s: NULL irq data\n", __func__);
  997. return IRQ_NONE;
  998. }
  999. if (unlikely((mbhc->mbhc_cb->lock_sleep(mbhc, true)) == false)) {
  1000. pr_warn("%s: failed to hold suspend\n", __func__);
  1001. r = IRQ_NONE;
  1002. } else {
  1003. /* Call handler */
  1004. wcd_mbhc_swch_irq_handler(mbhc);
  1005. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1006. }
  1007. pr_debug("%s: leave %d\n", __func__, r);
  1008. return r;
  1009. }
  1010. int wcd_mbhc_get_button_mask(struct wcd_mbhc *mbhc)
  1011. {
  1012. int mask = 0;
  1013. int btn;
  1014. btn = mbhc->mbhc_cb->map_btn_code_to_num(mbhc->component);
  1015. switch (btn) {
  1016. case 0:
  1017. mask = SND_JACK_BTN_0;
  1018. break;
  1019. case 1:
  1020. mask = SND_JACK_BTN_1;
  1021. break;
  1022. case 2:
  1023. mask = SND_JACK_BTN_2;
  1024. break;
  1025. case 3:
  1026. mask = SND_JACK_BTN_3;
  1027. break;
  1028. case 4:
  1029. mask = SND_JACK_BTN_4;
  1030. break;
  1031. case 5:
  1032. mask = SND_JACK_BTN_5;
  1033. break;
  1034. default:
  1035. break;
  1036. }
  1037. return mask;
  1038. }
  1039. EXPORT_SYMBOL(wcd_mbhc_get_button_mask);
  1040. static void wcd_btn_lpress_fn(struct work_struct *work)
  1041. {
  1042. struct delayed_work *dwork;
  1043. struct wcd_mbhc *mbhc;
  1044. s16 btn_result = 0;
  1045. pr_debug("%s: Enter\n", __func__);
  1046. dwork = to_delayed_work(work);
  1047. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_btn_dwork);
  1048. WCD_MBHC_REG_READ(WCD_MBHC_BTN_RESULT, btn_result);
  1049. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET) {
  1050. pr_debug("%s: Reporting long button press event, btn_result: %d\n",
  1051. __func__, btn_result);
  1052. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  1053. mbhc->buttons_pressed, mbhc->buttons_pressed);
  1054. }
  1055. pr_debug("%s: leave\n", __func__);
  1056. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1057. }
  1058. static bool wcd_mbhc_fw_validate(const void *data, size_t size)
  1059. {
  1060. u32 cfg_offset;
  1061. struct wcd_mbhc_btn_detect_cfg *btn_cfg;
  1062. struct firmware_cal fw;
  1063. fw.data = (void *)data;
  1064. fw.size = size;
  1065. if (fw.size < WCD_MBHC_CAL_MIN_SIZE)
  1066. return false;
  1067. /*
  1068. * Previous check guarantees that there is enough fw data up
  1069. * to num_btn
  1070. */
  1071. btn_cfg = WCD_MBHC_CAL_BTN_DET_PTR(fw.data);
  1072. cfg_offset = (u32) ((void *) btn_cfg - (void *) fw.data);
  1073. if (fw.size < (cfg_offset + WCD_MBHC_CAL_BTN_SZ(btn_cfg)))
  1074. return false;
  1075. return true;
  1076. }
  1077. static irqreturn_t wcd_mbhc_btn_press_handler(int irq, void *data)
  1078. {
  1079. struct wcd_mbhc *mbhc = data;
  1080. int mask;
  1081. unsigned long msec_val;
  1082. pr_debug("%s: enter\n", __func__);
  1083. complete(&mbhc->btn_press_compl);
  1084. WCD_MBHC_RSC_LOCK(mbhc);
  1085. wcd_cancel_btn_work(mbhc);
  1086. if (wcd_swch_level_remove(mbhc)) {
  1087. pr_debug("%s: Switch level is low ", __func__);
  1088. goto done;
  1089. }
  1090. mbhc->is_btn_press = true;
  1091. msec_val = jiffies_to_msecs(jiffies - mbhc->jiffies_atreport);
  1092. pr_debug("%s: msec_val = %ld\n", __func__, msec_val);
  1093. if (msec_val < MBHC_BUTTON_PRESS_THRESHOLD_MIN) {
  1094. pr_debug("%s: Too short, ignore button press\n", __func__);
  1095. goto done;
  1096. }
  1097. /* If switch interrupt already kicked in, ignore button press */
  1098. if (mbhc->in_swch_irq_handler) {
  1099. pr_debug("%s: Swtich level changed, ignore button press\n",
  1100. __func__);
  1101. goto done;
  1102. }
  1103. mask = wcd_mbhc_get_button_mask(mbhc);
  1104. if (mask == SND_JACK_BTN_0)
  1105. mbhc->btn_press_intr = true;
  1106. if (mbhc->current_plug != MBHC_PLUG_TYPE_HEADSET) {
  1107. pr_debug("%s: Plug isn't headset, ignore button press\n",
  1108. __func__);
  1109. goto done;
  1110. }
  1111. mbhc->buttons_pressed |= mask;
  1112. mbhc->mbhc_cb->lock_sleep(mbhc, true);
  1113. if (schedule_delayed_work(&mbhc->mbhc_btn_dwork,
  1114. msecs_to_jiffies(400)) == 0) {
  1115. WARN(1, "Button pressed twice without release event\n");
  1116. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1117. }
  1118. done:
  1119. pr_debug("%s: leave\n", __func__);
  1120. WCD_MBHC_RSC_UNLOCK(mbhc);
  1121. return IRQ_HANDLED;
  1122. }
  1123. static irqreturn_t wcd_mbhc_release_handler(int irq, void *data)
  1124. {
  1125. struct wcd_mbhc *mbhc = data;
  1126. int ret;
  1127. pr_debug("%s: enter\n", __func__);
  1128. WCD_MBHC_RSC_LOCK(mbhc);
  1129. if (wcd_swch_level_remove(mbhc)) {
  1130. pr_debug("%s: Switch level is low ", __func__);
  1131. goto exit;
  1132. }
  1133. if (mbhc->is_btn_press) {
  1134. mbhc->is_btn_press = false;
  1135. } else {
  1136. pr_debug("%s: This release is for fake btn press\n", __func__);
  1137. goto exit;
  1138. }
  1139. /*
  1140. * If current plug is headphone then there is no chance to
  1141. * get btn release interrupt, so connected cable should be
  1142. * headset not headphone.
  1143. * For ADC MBHC, ADC_COMPLETE interrupt will be generated
  1144. * in this case. So skip the check here.
  1145. */
  1146. if (mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY &&
  1147. mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  1148. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  1149. goto exit;
  1150. }
  1151. if (mbhc->buttons_pressed & WCD_MBHC_JACK_BUTTON_MASK) {
  1152. ret = wcd_cancel_btn_work(mbhc);
  1153. if (ret == 0) {
  1154. pr_debug("%s: Reporting long button release event\n",
  1155. __func__);
  1156. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  1157. 0, mbhc->buttons_pressed);
  1158. } else {
  1159. if (mbhc->in_swch_irq_handler) {
  1160. pr_debug("%s: Switch irq kicked in, ignore\n",
  1161. __func__);
  1162. } else {
  1163. pr_debug("%s: Reporting btn press\n",
  1164. __func__);
  1165. wcd_mbhc_jack_report(mbhc,
  1166. &mbhc->button_jack,
  1167. mbhc->buttons_pressed,
  1168. mbhc->buttons_pressed);
  1169. pr_debug("%s: Reporting btn release\n",
  1170. __func__);
  1171. wcd_mbhc_jack_report(mbhc,
  1172. &mbhc->button_jack,
  1173. 0, mbhc->buttons_pressed);
  1174. }
  1175. }
  1176. mbhc->buttons_pressed &= ~WCD_MBHC_JACK_BUTTON_MASK;
  1177. }
  1178. exit:
  1179. pr_debug("%s: leave\n", __func__);
  1180. WCD_MBHC_RSC_UNLOCK(mbhc);
  1181. return IRQ_HANDLED;
  1182. }
  1183. static irqreturn_t wcd_mbhc_hphl_ocp_irq(int irq, void *data)
  1184. {
  1185. struct wcd_mbhc *mbhc = data;
  1186. int val;
  1187. pr_debug("%s: received HPHL OCP irq\n", __func__);
  1188. if (mbhc) {
  1189. if (mbhc->mbhc_cb->hph_register_recovery) {
  1190. if (mbhc->mbhc_cb->hph_register_recovery(mbhc)) {
  1191. WCD_MBHC_REG_READ(WCD_MBHC_HPHR_OCP_STATUS,
  1192. val);
  1193. if ((val != -EINVAL) && val)
  1194. mbhc->is_hph_ocp_pending = true;
  1195. goto done;
  1196. }
  1197. }
  1198. if (mbhc->hphlocp_cnt < OCP_ATTEMPT) {
  1199. mbhc->hphlocp_cnt++;
  1200. pr_debug("%s: retry, hphlocp_cnt: %d\n", __func__,
  1201. mbhc->hphlocp_cnt);
  1202. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1203. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1204. } else {
  1205. mbhc->mbhc_cb->irq_control(mbhc->component,
  1206. mbhc->intr_ids->hph_left_ocp,
  1207. false);
  1208. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1209. mbhc->hph_status |= SND_JACK_OC_HPHL;
  1210. #endif /* CONFIG_AUDIO_QGKI */
  1211. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1212. mbhc->hph_status,
  1213. WCD_MBHC_JACK_MASK);
  1214. }
  1215. } else {
  1216. pr_err("%s: Bad wcd9xxx_spmi private data\n", __func__);
  1217. }
  1218. done:
  1219. return IRQ_HANDLED;
  1220. }
  1221. static irqreturn_t wcd_mbhc_hphr_ocp_irq(int irq, void *data)
  1222. {
  1223. struct wcd_mbhc *mbhc = data;
  1224. pr_debug("%s: received HPHR OCP irq\n", __func__);
  1225. if (!mbhc) {
  1226. pr_err("%s: Bad mbhc private data\n", __func__);
  1227. goto done;
  1228. }
  1229. if (mbhc->is_hph_ocp_pending) {
  1230. mbhc->is_hph_ocp_pending = false;
  1231. goto done;
  1232. }
  1233. if (mbhc->mbhc_cb->hph_register_recovery) {
  1234. if (mbhc->mbhc_cb->hph_register_recovery(mbhc))
  1235. /* register corruption, hence reset registers */
  1236. goto done;
  1237. }
  1238. if (mbhc->hphrocp_cnt < OCP_ATTEMPT) {
  1239. mbhc->hphrocp_cnt++;
  1240. pr_debug("%s: retry, hphrocp_cnt: %d\n", __func__,
  1241. mbhc->hphrocp_cnt);
  1242. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1243. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1244. } else {
  1245. mbhc->mbhc_cb->irq_control(mbhc->component,
  1246. mbhc->intr_ids->hph_right_ocp,
  1247. false);
  1248. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1249. mbhc->hph_status |= SND_JACK_OC_HPHR;
  1250. #endif /* CONFIG_AUDIO_QGKI */
  1251. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1252. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  1253. }
  1254. done:
  1255. return IRQ_HANDLED;
  1256. }
  1257. static int wcd_mbhc_initialise(struct wcd_mbhc *mbhc)
  1258. {
  1259. int ret = 0;
  1260. struct snd_soc_component *component = mbhc->component;
  1261. pr_debug("%s: enter\n", __func__);
  1262. WCD_MBHC_RSC_LOCK(mbhc);
  1263. /* enable HS detection */
  1264. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1265. mbhc->mbhc_cb->hph_pull_up_control_v2(component,
  1266. HS_PULLUP_I_DEFAULT);
  1267. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1268. mbhc->mbhc_cb->hph_pull_up_control(component, I_DEFAULT);
  1269. else
  1270. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL, 3);
  1271. /* Configure for moisture detection when duty cycle is not enabled.
  1272. * Otherwise disable moisture detection.
  1273. */
  1274. if (mbhc->mbhc_cfg->moisture_en && mbhc->mbhc_cb->mbhc_moisture_config
  1275. && !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  1276. mbhc->mbhc_cb->mbhc_moisture_config(mbhc);
  1277. else if (mbhc->mbhc_cb->mbhc_moisture_detect_en)
  1278. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc, false);
  1279. /*
  1280. * For USB analog we need to override the switch configuration.
  1281. * Also, disable hph_l pull-up current source as HS_DET_L is driven
  1282. * by an external source
  1283. */
  1284. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1285. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1286. mbhc->mbhc_cb->hph_pull_up_control_v2(component,
  1287. HS_PULLUP_I_OFF);
  1288. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1289. mbhc->mbhc_cb->hph_pull_up_control(component, I_OFF);
  1290. else
  1291. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL,
  1292. 0);
  1293. }
  1294. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_PLUG_TYPE, mbhc->hphl_swh);
  1295. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_PLUG_TYPE, mbhc->gnd_swh);
  1296. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_SW_HPH_LP_100K_TO_GND, 1);
  1297. if (mbhc->mbhc_cfg->gnd_det_en && mbhc->mbhc_cb->mbhc_gnd_det_ctrl)
  1298. mbhc->mbhc_cb->mbhc_gnd_det_ctrl(component, true);
  1299. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL, 1);
  1300. /*
  1301. * Disable L_DET for USB-C analog audio to avoid spurious interrupts
  1302. * when a non-audio accessory is inserted. L_DET_EN sets to 1 when FSA
  1303. * I2C driver notifies that ANALOG_AUDIO_ADAPTER is inserted
  1304. */
  1305. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1306. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  1307. else
  1308. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1309. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1310. /* Insertion debounce set to 48ms */
  1311. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 4);
  1312. } else {
  1313. /* Insertion debounce set to 96ms */
  1314. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 6);
  1315. }
  1316. /* Button Debounce set to 16ms */
  1317. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_DBNC, 2);
  1318. /* enable bias */
  1319. mbhc->mbhc_cb->mbhc_bias(component, true);
  1320. /* enable MBHC clock */
  1321. if (mbhc->mbhc_cb->clk_setup) {
  1322. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1323. mbhc->mbhc_cb->clk_setup(component, false);
  1324. else
  1325. mbhc->mbhc_cb->clk_setup(component, true);
  1326. }
  1327. /* program HS_VREF value */
  1328. wcd_program_hs_vref(mbhc);
  1329. wcd_program_btn_threshold(mbhc, false);
  1330. reinit_completion(&mbhc->btn_press_compl);
  1331. WCD_MBHC_RSC_UNLOCK(mbhc);
  1332. pr_debug("%s: leave\n", __func__);
  1333. return ret;
  1334. }
  1335. static void wcd_mbhc_fw_read(struct work_struct *work)
  1336. {
  1337. struct delayed_work *dwork;
  1338. struct wcd_mbhc *mbhc;
  1339. struct snd_soc_component *component;
  1340. const struct firmware *fw;
  1341. struct firmware_cal *fw_data = NULL;
  1342. int ret = -1, retry = 0;
  1343. bool use_default_cal = false;
  1344. dwork = to_delayed_work(work);
  1345. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_firmware_dwork);
  1346. component = mbhc->component;
  1347. while (retry < FW_READ_ATTEMPTS) {
  1348. retry++;
  1349. pr_debug("%s:Attempt %d to request MBHC firmware\n",
  1350. __func__, retry);
  1351. if (mbhc->mbhc_cb->get_hwdep_fw_cal)
  1352. fw_data = mbhc->mbhc_cb->get_hwdep_fw_cal(mbhc,
  1353. WCD9XXX_MBHC_CAL);
  1354. if (!fw_data)
  1355. ret = request_firmware(&fw, "wcd9320/wcd9320_mbhc.bin",
  1356. component->dev);
  1357. /*
  1358. * if request_firmware and hwdep cal both fail then
  1359. * sleep for 4sec for the userspace to send data to kernel
  1360. * retry for few times before bailing out
  1361. */
  1362. if ((ret != 0) && !fw_data) {
  1363. usleep_range(FW_READ_TIMEOUT, FW_READ_TIMEOUT +
  1364. WCD_MBHC_USLEEP_RANGE_MARGIN_US);
  1365. } else {
  1366. pr_debug("%s: MBHC Firmware read successful\n",
  1367. __func__);
  1368. break;
  1369. }
  1370. }
  1371. if (!fw_data)
  1372. pr_debug("%s: using request_firmware\n", __func__);
  1373. else
  1374. pr_debug("%s: using hwdep cal\n", __func__);
  1375. if (ret != 0 && !fw_data) {
  1376. pr_err("%s: Cannot load MBHC firmware use default cal\n",
  1377. __func__);
  1378. use_default_cal = true;
  1379. }
  1380. if (!use_default_cal) {
  1381. const void *data;
  1382. size_t size;
  1383. if (fw_data) {
  1384. data = fw_data->data;
  1385. size = fw_data->size;
  1386. } else {
  1387. data = fw->data;
  1388. size = fw->size;
  1389. }
  1390. if (wcd_mbhc_fw_validate(data, size) == false) {
  1391. pr_err("%s: Invalid MBHC cal data size use default cal\n",
  1392. __func__);
  1393. if (!fw_data)
  1394. release_firmware(fw);
  1395. } else {
  1396. if (fw_data) {
  1397. mbhc->mbhc_cfg->calibration =
  1398. (void *)fw_data->data;
  1399. mbhc->mbhc_cal = fw_data;
  1400. } else {
  1401. mbhc->mbhc_cfg->calibration =
  1402. (void *)fw->data;
  1403. mbhc->mbhc_fw = fw;
  1404. }
  1405. }
  1406. }
  1407. (void) wcd_mbhc_initialise(mbhc);
  1408. }
  1409. static int wcd_mbhc_set_keycode(struct wcd_mbhc *mbhc)
  1410. {
  1411. enum snd_jack_types type;
  1412. int i, ret, result = 0;
  1413. int *btn_key_code;
  1414. btn_key_code = mbhc->mbhc_cfg->key_code;
  1415. for (i = 0 ; i < WCD_MBHC_KEYCODE_NUM ; i++) {
  1416. if (btn_key_code[i] != 0) {
  1417. switch (i) {
  1418. case 0:
  1419. type = SND_JACK_BTN_0;
  1420. break;
  1421. case 1:
  1422. type = SND_JACK_BTN_1;
  1423. break;
  1424. case 2:
  1425. type = SND_JACK_BTN_2;
  1426. break;
  1427. case 3:
  1428. type = SND_JACK_BTN_3;
  1429. break;
  1430. case 4:
  1431. type = SND_JACK_BTN_4;
  1432. break;
  1433. case 5:
  1434. type = SND_JACK_BTN_5;
  1435. break;
  1436. default:
  1437. WARN_ONCE(1, "Wrong button number:%d\n", i);
  1438. result = -1;
  1439. return result;
  1440. }
  1441. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1442. type,
  1443. btn_key_code[i]);
  1444. if (ret) {
  1445. pr_err("%s: Failed to set code for %d\n",
  1446. __func__, btn_key_code[i]);
  1447. result = -1;
  1448. return result;
  1449. }
  1450. input_set_capability(
  1451. mbhc->button_jack.jack->input_dev,
  1452. EV_KEY, btn_key_code[i]);
  1453. pr_debug("%s: set btn%d key code:%d\n", __func__,
  1454. i, btn_key_code[i]);
  1455. }
  1456. }
  1457. if (btn_key_code[0])
  1458. mbhc->is_btn_already_regd = true;
  1459. return result;
  1460. }
  1461. #if IS_ENABLED(CONFIG_QCOM_FSA4480_I2C)
  1462. static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
  1463. unsigned long mode, void *ptr)
  1464. {
  1465. unsigned int l_det_en = 0;
  1466. unsigned int detection_type = 0;
  1467. struct wcd_mbhc *mbhc = container_of(nb, struct wcd_mbhc, fsa_nb);
  1468. if (!mbhc)
  1469. return -EINVAL;
  1470. dev_dbg(mbhc->component->dev, "%s: mode = %lu\n", __func__, mode);
  1471. if (mode == TYPEC_ACCESSORY_AUDIO) {
  1472. if (mbhc->mbhc_cb->clk_setup)
  1473. mbhc->mbhc_cb->clk_setup(mbhc->component, true);
  1474. /* insertion detected, enable L_DET_EN */
  1475. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1476. } else {
  1477. WCD_MBHC_REG_READ(WCD_MBHC_MECH_DETECTION_TYPE, detection_type);
  1478. WCD_MBHC_REG_READ(WCD_MBHC_L_DET_EN, l_det_en);
  1479. /* If both l_det_en and detection type are set, it means device was
  1480. * unplugged during SSR and detection interrupt was not handled.
  1481. * So trigger device disconnect */
  1482. if (detection_type && l_det_en) {
  1483. /* Set the detection type appropriately */
  1484. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  1485. !detection_type);
  1486. /* Set current plug type to the state before SSR */
  1487. mbhc->current_plug = mbhc->plug_before_ssr;
  1488. wcd_mbhc_swch_irq_handler(mbhc);
  1489. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1490. mbhc->plug_before_ssr = MBHC_PLUG_TYPE_NONE;
  1491. }
  1492. }
  1493. return 0;
  1494. }
  1495. #else
  1496. static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
  1497. unsigned long mode, void *ptr)
  1498. {
  1499. return 0;
  1500. }
  1501. #endif
  1502. int wcd_mbhc_start(struct wcd_mbhc *mbhc, struct wcd_mbhc_config *mbhc_cfg)
  1503. {
  1504. int rc = 0;
  1505. struct snd_soc_component *component;
  1506. struct snd_soc_card *card;
  1507. const char *usb_c_dt = "qcom,msm-mbhc-usbc-audio-supported";
  1508. if (!mbhc || !mbhc_cfg)
  1509. return -EINVAL;
  1510. component = mbhc->component;
  1511. card = component->card;
  1512. /* update the mbhc config */
  1513. mbhc->mbhc_cfg = mbhc_cfg;
  1514. dev_dbg(mbhc->component->dev, "%s: enter\n", __func__);
  1515. /* check if USB C analog is defined on device tree */
  1516. mbhc_cfg->enable_usbc_analog = 0;
  1517. if (of_find_property(card->dev->of_node, usb_c_dt, NULL)) {
  1518. rc = of_property_read_u32(card->dev->of_node, usb_c_dt,
  1519. &mbhc_cfg->enable_usbc_analog);
  1520. }
  1521. if (mbhc_cfg->enable_usbc_analog == 0 || rc != 0) {
  1522. dev_dbg(card->dev,
  1523. "%s: %s in dt node is missing or false\n",
  1524. __func__, usb_c_dt);
  1525. dev_dbg(card->dev,
  1526. "%s: skipping USB c analog configuration\n", __func__);
  1527. }
  1528. /* Parse fsa switch handle */
  1529. if (mbhc_cfg->enable_usbc_analog) {
  1530. dev_dbg(mbhc->component->dev, "%s: usbc analog enabled\n",
  1531. __func__);
  1532. mbhc->swap_thr = GND_MIC_USBC_SWAP_THRESHOLD;
  1533. mbhc->fsa_np = of_parse_phandle(card->dev->of_node,
  1534. "fsa4480-i2c-handle", 0);
  1535. if (!mbhc->fsa_np) {
  1536. dev_err(card->dev, "%s: fsa4480 i2c node not found\n",
  1537. __func__);
  1538. rc = -EINVAL;
  1539. goto err;
  1540. }
  1541. }
  1542. /* Set btn key code */
  1543. if ((!mbhc->is_btn_already_regd) && wcd_mbhc_set_keycode(mbhc))
  1544. pr_err("Set btn key code error!!!\n");
  1545. if (!mbhc->mbhc_cfg->read_fw_bin ||
  1546. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_fw) ||
  1547. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_cal)) {
  1548. rc = wcd_mbhc_initialise(mbhc);
  1549. if (rc) {
  1550. dev_err(card->dev, "%s: wcd mbhc initialize failed\n",
  1551. __func__);
  1552. goto err;
  1553. }
  1554. } else {
  1555. if (!mbhc->mbhc_fw || !mbhc->mbhc_cal)
  1556. schedule_delayed_work(&mbhc->mbhc_firmware_dwork,
  1557. usecs_to_jiffies(FW_READ_TIMEOUT));
  1558. else
  1559. pr_err("%s: Skipping to read mbhc fw, 0x%pK %pK\n",
  1560. __func__, mbhc->mbhc_fw, mbhc->mbhc_cal);
  1561. }
  1562. if (mbhc_cfg->enable_usbc_analog) {
  1563. mbhc->fsa_nb.notifier_call = wcd_mbhc_usbc_ana_event_handler;
  1564. mbhc->fsa_nb.priority = 0;
  1565. rc = fsa4480_reg_notifier(&mbhc->fsa_nb, mbhc->fsa_np);
  1566. }
  1567. return rc;
  1568. err:
  1569. dev_dbg(mbhc->component->dev, "%s: leave %d\n", __func__, rc);
  1570. return rc;
  1571. }
  1572. EXPORT_SYMBOL(wcd_mbhc_start);
  1573. void wcd_mbhc_stop(struct wcd_mbhc *mbhc)
  1574. {
  1575. pr_debug("%s: enter\n", __func__);
  1576. if (mbhc->current_plug != MBHC_PLUG_TYPE_NONE) {
  1577. if (mbhc->mbhc_cb && mbhc->mbhc_cb->skip_imped_detect)
  1578. mbhc->mbhc_cb->skip_imped_detect(mbhc->component);
  1579. }
  1580. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1581. mbhc->hph_status = 0;
  1582. if (mbhc->mbhc_cb && mbhc->mbhc_cb->irq_control) {
  1583. mbhc->mbhc_cb->irq_control(mbhc->component,
  1584. mbhc->intr_ids->hph_left_ocp,
  1585. false);
  1586. mbhc->mbhc_cb->irq_control(mbhc->component,
  1587. mbhc->intr_ids->hph_right_ocp,
  1588. false);
  1589. }
  1590. if (mbhc->mbhc_fw || mbhc->mbhc_cal) {
  1591. cancel_delayed_work_sync(&mbhc->mbhc_firmware_dwork);
  1592. if (!mbhc->mbhc_cal)
  1593. release_firmware(mbhc->mbhc_fw);
  1594. mbhc->mbhc_fw = NULL;
  1595. mbhc->mbhc_cal = NULL;
  1596. }
  1597. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1598. fsa4480_unreg_notifier(&mbhc->fsa_nb, mbhc->fsa_np);
  1599. pr_debug("%s: leave\n", __func__);
  1600. }
  1601. EXPORT_SYMBOL(wcd_mbhc_stop);
  1602. /*
  1603. * wcd_mbhc_init : initialize MBHC internal structures.
  1604. *
  1605. * NOTE: mbhc->mbhc_cfg is not YET configure so shouldn't be used
  1606. */
  1607. int wcd_mbhc_init(struct wcd_mbhc *mbhc, struct snd_soc_component *component,
  1608. const struct wcd_mbhc_cb *mbhc_cb,
  1609. const struct wcd_mbhc_intr *mbhc_cdc_intr_ids,
  1610. struct wcd_mbhc_register *wcd_mbhc_regs,
  1611. bool impedance_det_en)
  1612. {
  1613. int ret = 0;
  1614. int hph_swh = 0;
  1615. int gnd_swh = 0;
  1616. u32 hph_moist_config[3];
  1617. struct snd_soc_card *card = component->card;
  1618. const char *hph_switch = "qcom,msm-mbhc-hphl-swh";
  1619. const char *gnd_switch = "qcom,msm-mbhc-gnd-swh";
  1620. const char *hs_thre = "qcom,msm-mbhc-hs-mic-max-threshold-mv";
  1621. const char *hph_thre = "qcom,msm-mbhc-hs-mic-min-threshold-mv";
  1622. pr_debug("%s: enter\n", __func__);
  1623. ret = of_property_read_u32(card->dev->of_node, hph_switch, &hph_swh);
  1624. if (ret) {
  1625. dev_err(card->dev,
  1626. "%s: missing %s in dt node\n", __func__, hph_switch);
  1627. goto err;
  1628. }
  1629. ret = of_property_read_u32(card->dev->of_node, gnd_switch, &gnd_swh);
  1630. if (ret) {
  1631. dev_err(card->dev,
  1632. "%s: missing %s in dt node\n", __func__, gnd_switch);
  1633. goto err;
  1634. }
  1635. ret = of_property_read_u32(card->dev->of_node, hs_thre,
  1636. &(mbhc->hs_thr));
  1637. if (ret)
  1638. dev_dbg(card->dev,
  1639. "%s: missing %s in dt node\n", __func__, hs_thre);
  1640. ret = of_property_read_u32(card->dev->of_node, hph_thre,
  1641. &(mbhc->hph_thr));
  1642. if (ret)
  1643. dev_dbg(card->dev,
  1644. "%s: missing %s in dt node\n", __func__, hph_thre);
  1645. ret = of_property_read_u32_array(card->dev->of_node,
  1646. "qcom,msm-mbhc-moist-cfg",
  1647. hph_moist_config, 3);
  1648. if (ret) {
  1649. dev_dbg(card->dev, "%s: no qcom,msm-mbhc-moist-cfg in DT\n",
  1650. __func__);
  1651. mbhc->moist_vref = V_45_MV;
  1652. mbhc->moist_iref = I_3P0_UA;
  1653. mbhc->moist_rref = R_24_KOHM;
  1654. } else {
  1655. mbhc->moist_vref = hph_moist_config[0];
  1656. mbhc->moist_iref = hph_moist_config[1];
  1657. mbhc->moist_rref = hph_moist_config[2];
  1658. }
  1659. mbhc->in_swch_irq_handler = false;
  1660. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1661. mbhc->is_btn_press = false;
  1662. mbhc->component = component;
  1663. mbhc->intr_ids = mbhc_cdc_intr_ids;
  1664. mbhc->impedance_detect = impedance_det_en;
  1665. mbhc->hphl_swh = hph_swh;
  1666. mbhc->gnd_swh = gnd_swh;
  1667. mbhc->micbias_enable = false;
  1668. mbhc->mbhc_cb = mbhc_cb;
  1669. mbhc->btn_press_intr = false;
  1670. mbhc->is_hs_recording = false;
  1671. mbhc->is_extn_cable = false;
  1672. mbhc->extn_cable_hph_rem = false;
  1673. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  1674. mbhc->wcd_mbhc_regs = wcd_mbhc_regs;
  1675. mbhc->swap_thr = GND_MIC_SWAP_THRESHOLD;
  1676. mbhc->hphl_cross_conn_thr = HPHL_CROSS_CONN_THRESHOLD;
  1677. mbhc->hphr_cross_conn_thr = HPHR_CROSS_CONN_THRESHOLD;
  1678. if (mbhc->intr_ids == NULL) {
  1679. pr_err("%s: Interrupt mapping not provided\n", __func__);
  1680. return -EINVAL;
  1681. }
  1682. if (!mbhc->wcd_mbhc_regs) {
  1683. dev_err(component->dev, "%s: mbhc registers are not defined\n",
  1684. __func__);
  1685. return -EINVAL;
  1686. }
  1687. /* Check if IRQ and other required callbacks are defined or not */
  1688. if (!mbhc_cb || !mbhc_cb->request_irq || !mbhc_cb->irq_control ||
  1689. !mbhc_cb->free_irq || !mbhc_cb->map_btn_code_to_num ||
  1690. !mbhc_cb->lock_sleep || !mbhc_cb->mbhc_bias ||
  1691. !mbhc_cb->set_btn_thr) {
  1692. dev_err(component->dev, "%s: required mbhc callbacks are not defined\n",
  1693. __func__);
  1694. return -EINVAL;
  1695. }
  1696. /* No need to create new sound card jacks if is is already created */
  1697. if (mbhc->headset_jack.jack == NULL) {
  1698. ret = snd_soc_card_jack_new(component->card,
  1699. "Headset Jack", WCD_MBHC_JACK_MASK,
  1700. &mbhc->headset_jack, NULL, 0);
  1701. if (ret) {
  1702. pr_err("%s: Failed to create new jack\n", __func__);
  1703. return ret;
  1704. }
  1705. ret = snd_soc_card_jack_new(component->card,
  1706. "Button Jack",
  1707. WCD_MBHC_JACK_BUTTON_MASK,
  1708. &mbhc->button_jack, NULL, 0);
  1709. if (ret) {
  1710. pr_err("Failed to create new jack\n");
  1711. return ret;
  1712. }
  1713. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1714. SND_JACK_BTN_0,
  1715. KEY_MEDIA);
  1716. if (ret) {
  1717. pr_err("%s: Failed to set code for btn-0\n",
  1718. __func__);
  1719. return ret;
  1720. }
  1721. INIT_DELAYED_WORK(&mbhc->mbhc_firmware_dwork,
  1722. wcd_mbhc_fw_read);
  1723. INIT_DELAYED_WORK(&mbhc->mbhc_btn_dwork, wcd_btn_lpress_fn);
  1724. }
  1725. mutex_init(&mbhc->hphl_pa_lock);
  1726. mutex_init(&mbhc->hphr_pa_lock);
  1727. init_completion(&mbhc->btn_press_compl);
  1728. /* Register event notifier */
  1729. mbhc->nblock.notifier_call = wcd_event_notify;
  1730. if (mbhc->mbhc_cb->register_notifier) {
  1731. ret = mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock,
  1732. true);
  1733. if (ret) {
  1734. pr_err("%s: Failed to register notifier %d\n",
  1735. __func__, ret);
  1736. return ret;
  1737. }
  1738. }
  1739. init_waitqueue_head(&mbhc->wait_btn_press);
  1740. mutex_init(&mbhc->codec_resource_lock);
  1741. switch (mbhc->mbhc_detection_logic) {
  1742. case WCD_DETECTION_LEGACY:
  1743. wcd_mbhc_legacy_init(mbhc);
  1744. break;
  1745. case WCD_DETECTION_ADC:
  1746. wcd_mbhc_adc_init(mbhc);
  1747. break;
  1748. default:
  1749. pr_err("%s: Unknown detection logic type %d\n",
  1750. __func__, mbhc->mbhc_detection_logic);
  1751. break;
  1752. }
  1753. if (!mbhc->mbhc_fn ||
  1754. !mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq ||
  1755. !mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq ||
  1756. !mbhc->mbhc_fn->wcd_mbhc_detect_plug_type ||
  1757. !mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1758. pr_err("%s: mbhc function pointer is NULL\n", __func__);
  1759. goto err_mbhc_sw_irq;
  1760. }
  1761. ret = mbhc->mbhc_cb->request_irq(component,
  1762. mbhc->intr_ids->mbhc_sw_intr,
  1763. wcd_mbhc_mech_plug_detect_irq,
  1764. "mbhc sw intr", mbhc);
  1765. if (ret) {
  1766. pr_err("%s: Failed to request irq %d, ret = %d\n", __func__,
  1767. mbhc->intr_ids->mbhc_sw_intr, ret);
  1768. goto err_mbhc_sw_irq;
  1769. }
  1770. ret = mbhc->mbhc_cb->request_irq(component,
  1771. mbhc->intr_ids->mbhc_btn_press_intr,
  1772. wcd_mbhc_btn_press_handler,
  1773. "Button Press detect", mbhc);
  1774. if (ret) {
  1775. pr_err("%s: Failed to request irq %d\n", __func__,
  1776. mbhc->intr_ids->mbhc_btn_press_intr);
  1777. goto err_btn_press_irq;
  1778. }
  1779. ret = mbhc->mbhc_cb->request_irq(component,
  1780. mbhc->intr_ids->mbhc_btn_release_intr,
  1781. wcd_mbhc_release_handler,
  1782. "Button Release detect", mbhc);
  1783. if (ret) {
  1784. pr_err("%s: Failed to request irq %d\n", __func__,
  1785. mbhc->intr_ids->mbhc_btn_release_intr);
  1786. goto err_btn_release_irq;
  1787. }
  1788. ret = mbhc->mbhc_cb->request_irq(component,
  1789. mbhc->intr_ids->mbhc_hs_ins_intr,
  1790. mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq,
  1791. "Elect Insert", mbhc);
  1792. if (ret) {
  1793. pr_err("%s: Failed to request irq %d\n", __func__,
  1794. mbhc->intr_ids->mbhc_hs_ins_intr);
  1795. goto err_mbhc_hs_ins_irq;
  1796. }
  1797. mbhc->mbhc_cb->irq_control(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1798. false);
  1799. clear_bit(WCD_MBHC_ELEC_HS_INS, &mbhc->intr_status);
  1800. ret = mbhc->mbhc_cb->request_irq(component,
  1801. mbhc->intr_ids->mbhc_hs_rem_intr,
  1802. mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq,
  1803. "Elect Remove", mbhc);
  1804. if (ret) {
  1805. pr_err("%s: Failed to request irq %d\n", __func__,
  1806. mbhc->intr_ids->mbhc_hs_rem_intr);
  1807. goto err_mbhc_hs_rem_irq;
  1808. }
  1809. mbhc->mbhc_cb->irq_control(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  1810. false);
  1811. clear_bit(WCD_MBHC_ELEC_HS_REM, &mbhc->intr_status);
  1812. ret = mbhc->mbhc_cb->request_irq(component,
  1813. mbhc->intr_ids->hph_left_ocp,
  1814. wcd_mbhc_hphl_ocp_irq, "HPH_L OCP detect",
  1815. mbhc);
  1816. if (ret) {
  1817. pr_err("%s: Failed to request irq %d\n", __func__,
  1818. mbhc->intr_ids->hph_left_ocp);
  1819. goto err_hphl_ocp_irq;
  1820. }
  1821. ret = mbhc->mbhc_cb->request_irq(component,
  1822. mbhc->intr_ids->hph_right_ocp,
  1823. wcd_mbhc_hphr_ocp_irq, "HPH_R OCP detect",
  1824. mbhc);
  1825. if (ret) {
  1826. pr_err("%s: Failed to request irq %d\n", __func__,
  1827. mbhc->intr_ids->hph_right_ocp);
  1828. goto err_hphr_ocp_irq;
  1829. }
  1830. mbhc->deinit_in_progress = false;
  1831. pr_debug("%s: leave ret %d\n", __func__, ret);
  1832. return ret;
  1833. err_hphr_ocp_irq:
  1834. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_left_ocp, mbhc);
  1835. err_hphl_ocp_irq:
  1836. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  1837. mbhc);
  1838. err_mbhc_hs_rem_irq:
  1839. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1840. mbhc);
  1841. err_mbhc_hs_ins_irq:
  1842. mbhc->mbhc_cb->free_irq(component,
  1843. mbhc->intr_ids->mbhc_btn_release_intr,
  1844. mbhc);
  1845. err_btn_release_irq:
  1846. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_btn_press_intr,
  1847. mbhc);
  1848. err_btn_press_irq:
  1849. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1850. err_mbhc_sw_irq:
  1851. if (mbhc->mbhc_cb->register_notifier)
  1852. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1853. mutex_destroy(&mbhc->codec_resource_lock);
  1854. err:
  1855. pr_debug("%s: leave ret %d\n", __func__, ret);
  1856. return ret;
  1857. }
  1858. EXPORT_SYMBOL(wcd_mbhc_init);
  1859. void wcd_mbhc_deinit(struct wcd_mbhc *mbhc)
  1860. {
  1861. struct snd_soc_component *component = mbhc->component;
  1862. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1863. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_btn_press_intr,
  1864. mbhc);
  1865. mbhc->mbhc_cb->free_irq(component,
  1866. mbhc->intr_ids->mbhc_btn_release_intr,
  1867. mbhc);
  1868. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_ins_intr,
  1869. mbhc);
  1870. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->mbhc_hs_rem_intr,
  1871. mbhc);
  1872. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_left_ocp, mbhc);
  1873. mbhc->mbhc_cb->free_irq(component, mbhc->intr_ids->hph_right_ocp, mbhc);
  1874. if (mbhc->mbhc_cb && mbhc->mbhc_cb->register_notifier)
  1875. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1876. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1877. WCD_MBHC_RSC_LOCK(mbhc);
  1878. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  1879. &mbhc->correct_plug_swch);
  1880. WCD_MBHC_RSC_UNLOCK(mbhc);
  1881. }
  1882. mutex_destroy(&mbhc->codec_resource_lock);
  1883. mutex_destroy(&mbhc->hphl_pa_lock);
  1884. mutex_destroy(&mbhc->hphr_pa_lock);
  1885. }
  1886. EXPORT_SYMBOL(wcd_mbhc_deinit);
  1887. static int __init mbhc_init(void)
  1888. {
  1889. return 0;
  1890. }
  1891. static void __exit mbhc_exit(void)
  1892. {
  1893. }
  1894. module_init(mbhc_init);
  1895. module_exit(mbhc_exit);
  1896. MODULE_DESCRIPTION("wcd MBHC v2 module");
  1897. MODULE_LICENSE("GPL v2");