wcd-mbhc-v2.c 60 KB

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