wcd-mbhc-v2.c 56 KB

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