wcd-mbhc-v2.c 65 KB

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