wcd-mbhc-v2.c 59 KB

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