wcd-mbhc-v2.c 59 KB

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