wcd-mbhc-v2.c 64 KB

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