wcd-mbhc-v2.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  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. if (mbhc->deinit_in_progress) {
  712. pr_info("%s: mbhc deinit in progess: ignore report\n", __func__);
  713. return;
  714. }
  715. pr_debug("%s: enter current_plug(%d) new_plug(%d)\n",
  716. __func__, mbhc->current_plug, plug_type);
  717. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  718. if (mbhc->current_plug == plug_type) {
  719. pr_debug("%s: cable already reported, exit\n", __func__);
  720. goto exit;
  721. }
  722. if (plug_type == MBHC_PLUG_TYPE_HEADPHONE) {
  723. /*
  724. * Nothing was reported previously
  725. * report a headphone or unsupported
  726. */
  727. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_HEADPHONE);
  728. } else if (plug_type == MBHC_PLUG_TYPE_GND_MIC_SWAP) {
  729. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE)
  730. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADPHONE);
  731. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET)
  732. wcd_mbhc_report_plug(mbhc, 0, SND_JACK_HEADSET);
  733. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_UNSUPPORTED);
  734. } else if (plug_type == MBHC_PLUG_TYPE_HEADSET) {
  735. if (mbhc->mbhc_cfg->enable_anc_mic_detect &&
  736. mbhc->mbhc_fn->wcd_mbhc_detect_anc_plug_type)
  737. anc_mic_found =
  738. mbhc->mbhc_fn->wcd_mbhc_detect_anc_plug_type(mbhc);
  739. jack_type = SND_JACK_HEADSET;
  740. if (anc_mic_found)
  741. jack_type = SND_JACK_ANC_HEADPHONE;
  742. /*
  743. * If Headphone was reported previously, this will
  744. * only report the mic line
  745. */
  746. wcd_mbhc_report_plug(mbhc, 1, jack_type);
  747. } else if (plug_type == MBHC_PLUG_TYPE_HIGH_HPH) {
  748. if (mbhc->mbhc_cfg->detect_extn_cable) {
  749. /* High impedance device found. Report as LINEOUT */
  750. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  751. pr_debug("%s: setup mic trigger for further detection\n",
  752. __func__);
  753. /* Disable HW FSM and current source */
  754. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  755. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  756. /* Setup for insertion detection */
  757. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE,
  758. 1);
  759. /*
  760. * Enable HPHL trigger and MIC Schmitt triggers
  761. * and request for elec insertion interrupts
  762. */
  763. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC,
  764. 3);
  765. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS,
  766. true);
  767. } else {
  768. wcd_mbhc_report_plug(mbhc, 1, SND_JACK_LINEOUT);
  769. }
  770. } else {
  771. WARN(1, "Unexpected current plug_type %d, plug_type %d\n",
  772. mbhc->current_plug, plug_type);
  773. }
  774. exit:
  775. pr_debug("%s: leave\n", __func__);
  776. }
  777. EXPORT_SYMBOL(wcd_mbhc_find_plug_and_report);
  778. static bool wcd_mbhc_moisture_detect(struct wcd_mbhc *mbhc, bool detection_type)
  779. {
  780. bool ret = false;
  781. if (!mbhc->mbhc_cfg->moisture_en ||
  782. !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  783. return ret;
  784. if (!mbhc->mbhc_cb->mbhc_get_moisture_status ||
  785. !mbhc->mbhc_cb->mbhc_moisture_polling_ctrl ||
  786. !mbhc->mbhc_cb->mbhc_moisture_detect_en)
  787. return ret;
  788. if (mbhc->mbhc_cb->mbhc_get_moisture_status(mbhc)) {
  789. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  790. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_DET_EN, 0);
  791. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc, true);
  792. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  793. detection_type);
  794. ret = true;
  795. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  796. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_DET_EN, 1);
  797. } else {
  798. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc, false);
  799. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc, false);
  800. }
  801. return ret;
  802. }
  803. static void wcd_mbhc_swch_irq_handler(struct wcd_mbhc *mbhc)
  804. {
  805. bool detection_type = 0;
  806. bool micbias1 = false;
  807. struct snd_soc_codec *codec = mbhc->codec;
  808. enum snd_jack_types jack_type;
  809. dev_dbg(codec->dev, "%s: enter\n", __func__);
  810. WCD_MBHC_RSC_LOCK(mbhc);
  811. mbhc->in_swch_irq_handler = true;
  812. /* cancel pending button press */
  813. if (wcd_cancel_btn_work(mbhc))
  814. pr_debug("%s: button press is canceled\n", __func__);
  815. WCD_MBHC_REG_READ(WCD_MBHC_MECH_DETECTION_TYPE, detection_type);
  816. /* Set the detection type appropriately */
  817. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_MECH_DETECTION_TYPE,
  818. !detection_type);
  819. pr_debug("%s: mbhc->current_plug: %d detection_type: %d\n", __func__,
  820. mbhc->current_plug, detection_type);
  821. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug)
  822. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  823. &mbhc->correct_plug_swch);
  824. else
  825. pr_info("%s: hs_detect_plug work not cancelled\n", __func__);
  826. if (mbhc->mbhc_cb->micbias_enable_status)
  827. micbias1 = mbhc->mbhc_cb->micbias_enable_status(mbhc,
  828. MIC_BIAS_1);
  829. if ((mbhc->current_plug == MBHC_PLUG_TYPE_NONE) &&
  830. detection_type) {
  831. /* If moisture is present, then enable polling, disable
  832. * moisture detection and wait for interrupt
  833. */
  834. if (wcd_mbhc_moisture_detect(mbhc, detection_type))
  835. goto done;
  836. /* Make sure MASTER_BIAS_CTL is enabled */
  837. mbhc->mbhc_cb->mbhc_bias(codec, true);
  838. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  839. mbhc->mbhc_cb->mbhc_common_micb_ctrl(codec,
  840. MBHC_COMMON_MICB_TAIL_CURR, true);
  841. if (!mbhc->mbhc_cfg->hs_ext_micbias &&
  842. mbhc->mbhc_cb->micb_internal)
  843. /*
  844. * Enable Tx2 RBias if the headset
  845. * is using internal micbias
  846. */
  847. mbhc->mbhc_cb->micb_internal(codec, 1, true);
  848. /* Remove micbias pulldown */
  849. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 0);
  850. /* Apply trim if needed on the device */
  851. if (mbhc->mbhc_cb->trim_btn_reg)
  852. mbhc->mbhc_cb->trim_btn_reg(codec);
  853. /* Enable external voltage source to micbias if present */
  854. if (mbhc->mbhc_cb->enable_mb_source)
  855. mbhc->mbhc_cb->enable_mb_source(mbhc, true);
  856. mbhc->btn_press_intr = false;
  857. mbhc->is_btn_press = false;
  858. if (mbhc->mbhc_fn)
  859. mbhc->mbhc_fn->wcd_mbhc_detect_plug_type(mbhc);
  860. } else if ((mbhc->current_plug != MBHC_PLUG_TYPE_NONE)
  861. && !detection_type) {
  862. /* Disable external voltage source to micbias if present */
  863. if (mbhc->mbhc_cb->enable_mb_source)
  864. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  865. /* Disable HW FSM */
  866. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  867. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  868. if (mbhc->mbhc_cb->mbhc_common_micb_ctrl)
  869. mbhc->mbhc_cb->mbhc_common_micb_ctrl(codec,
  870. MBHC_COMMON_MICB_TAIL_CURR, false);
  871. if (mbhc->mbhc_cb->set_cap_mode)
  872. mbhc->mbhc_cb->set_cap_mode(codec, micbias1, false);
  873. mbhc->btn_press_intr = false;
  874. mbhc->is_btn_press = false;
  875. switch (mbhc->current_plug) {
  876. case MBHC_PLUG_TYPE_HEADPHONE:
  877. jack_type = SND_JACK_HEADPHONE;
  878. break;
  879. case MBHC_PLUG_TYPE_GND_MIC_SWAP:
  880. jack_type = SND_JACK_UNSUPPORTED;
  881. break;
  882. case MBHC_PLUG_TYPE_HEADSET:
  883. /* make sure to turn off Rbias */
  884. if (mbhc->mbhc_cb->micb_internal)
  885. mbhc->mbhc_cb->micb_internal(codec, 1, false);
  886. /* Pulldown micbias */
  887. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_PULLDOWN_CTRL, 1);
  888. jack_type = SND_JACK_HEADSET;
  889. break;
  890. case MBHC_PLUG_TYPE_HIGH_HPH:
  891. if (mbhc->mbhc_detection_logic == WCD_DETECTION_ADC)
  892. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_ISRC_EN, 0);
  893. mbhc->is_extn_cable = false;
  894. jack_type = SND_JACK_LINEOUT;
  895. break;
  896. case MBHC_PLUG_TYPE_ANC_HEADPHONE:
  897. jack_type = SND_JACK_ANC_HEADPHONE;
  898. break;
  899. default:
  900. pr_info("%s: Invalid current plug: %d\n",
  901. __func__, mbhc->current_plug);
  902. jack_type = SND_JACK_UNSUPPORTED;
  903. break;
  904. }
  905. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_REM, false);
  906. wcd_mbhc_hs_elec_irq(mbhc, WCD_MBHC_ELEC_HS_INS, false);
  907. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_DETECTION_TYPE, 1);
  908. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_ELECT_SCHMT_ISRC, 0);
  909. mbhc->extn_cable_hph_rem = false;
  910. wcd_mbhc_report_plug(mbhc, 0, jack_type);
  911. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  912. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  913. if (mbhc->mbhc_cb->clk_setup)
  914. mbhc->mbhc_cb->clk_setup(mbhc->codec, false);
  915. }
  916. if (mbhc->mbhc_cfg->moisture_en &&
  917. mbhc->mbhc_cfg->moisture_duty_cycle_en) {
  918. if (mbhc->mbhc_cb->mbhc_moisture_polling_ctrl)
  919. mbhc->mbhc_cb->mbhc_moisture_polling_ctrl(mbhc,
  920. false);
  921. if (mbhc->mbhc_cb->mbhc_moisture_detect_en)
  922. mbhc->mbhc_cb->mbhc_moisture_detect_en(mbhc,
  923. false);
  924. }
  925. } else if (!detection_type) {
  926. /* Disable external voltage source to micbias if present */
  927. if (mbhc->mbhc_cb->enable_mb_source)
  928. mbhc->mbhc_cb->enable_mb_source(mbhc, false);
  929. /* Disable HW FSM */
  930. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_FSM_EN, 0);
  931. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_ISRC_CTL, 0);
  932. mbhc->extn_cable_hph_rem = false;
  933. }
  934. done:
  935. mbhc->in_swch_irq_handler = false;
  936. WCD_MBHC_RSC_UNLOCK(mbhc);
  937. pr_debug("%s: leave\n", __func__);
  938. }
  939. static irqreturn_t wcd_mbhc_mech_plug_detect_irq(int irq, void *data)
  940. {
  941. int r = IRQ_HANDLED;
  942. struct wcd_mbhc *mbhc = data;
  943. pr_debug("%s: enter\n", __func__);
  944. if (unlikely((mbhc->mbhc_cb->lock_sleep(mbhc, true)) == false)) {
  945. pr_warn("%s: failed to hold suspend\n", __func__);
  946. r = IRQ_NONE;
  947. } else {
  948. /* Call handler */
  949. wcd_mbhc_swch_irq_handler(mbhc);
  950. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  951. }
  952. pr_debug("%s: leave %d\n", __func__, r);
  953. return r;
  954. }
  955. int wcd_mbhc_get_button_mask(struct wcd_mbhc *mbhc)
  956. {
  957. int mask = 0;
  958. int btn;
  959. btn = mbhc->mbhc_cb->map_btn_code_to_num(mbhc->codec);
  960. switch (btn) {
  961. case 0:
  962. mask = SND_JACK_BTN_0;
  963. break;
  964. case 1:
  965. mask = SND_JACK_BTN_1;
  966. break;
  967. case 2:
  968. mask = SND_JACK_BTN_2;
  969. break;
  970. case 3:
  971. mask = SND_JACK_BTN_3;
  972. break;
  973. case 4:
  974. mask = SND_JACK_BTN_4;
  975. break;
  976. case 5:
  977. mask = SND_JACK_BTN_5;
  978. break;
  979. default:
  980. break;
  981. }
  982. return mask;
  983. }
  984. EXPORT_SYMBOL(wcd_mbhc_get_button_mask);
  985. static void wcd_btn_lpress_fn(struct work_struct *work)
  986. {
  987. struct delayed_work *dwork;
  988. struct wcd_mbhc *mbhc;
  989. s16 btn_result = 0;
  990. pr_debug("%s: Enter\n", __func__);
  991. dwork = to_delayed_work(work);
  992. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_btn_dwork);
  993. WCD_MBHC_REG_READ(WCD_MBHC_BTN_RESULT, btn_result);
  994. if (mbhc->current_plug == MBHC_PLUG_TYPE_HEADSET) {
  995. pr_debug("%s: Reporting long button press event, btn_result: %d\n",
  996. __func__, btn_result);
  997. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  998. mbhc->buttons_pressed, mbhc->buttons_pressed);
  999. }
  1000. pr_debug("%s: leave\n", __func__);
  1001. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1002. }
  1003. static bool wcd_mbhc_fw_validate(const void *data, size_t size)
  1004. {
  1005. u32 cfg_offset;
  1006. struct wcd_mbhc_btn_detect_cfg *btn_cfg;
  1007. struct firmware_cal fw;
  1008. fw.data = (void *)data;
  1009. fw.size = size;
  1010. if (fw.size < WCD_MBHC_CAL_MIN_SIZE)
  1011. return false;
  1012. /*
  1013. * Previous check guarantees that there is enough fw data up
  1014. * to num_btn
  1015. */
  1016. btn_cfg = WCD_MBHC_CAL_BTN_DET_PTR(fw.data);
  1017. cfg_offset = (u32) ((void *) btn_cfg - (void *) fw.data);
  1018. if (fw.size < (cfg_offset + WCD_MBHC_CAL_BTN_SZ(btn_cfg)))
  1019. return false;
  1020. return true;
  1021. }
  1022. static irqreturn_t wcd_mbhc_btn_press_handler(int irq, void *data)
  1023. {
  1024. struct wcd_mbhc *mbhc = data;
  1025. int mask;
  1026. unsigned long msec_val;
  1027. pr_debug("%s: enter\n", __func__);
  1028. complete(&mbhc->btn_press_compl);
  1029. WCD_MBHC_RSC_LOCK(mbhc);
  1030. wcd_cancel_btn_work(mbhc);
  1031. if (wcd_swch_level_remove(mbhc)) {
  1032. pr_debug("%s: Switch level is low ", __func__);
  1033. goto done;
  1034. }
  1035. mbhc->is_btn_press = true;
  1036. msec_val = jiffies_to_msecs(jiffies - mbhc->jiffies_atreport);
  1037. pr_debug("%s: msec_val = %ld\n", __func__, msec_val);
  1038. if (msec_val < MBHC_BUTTON_PRESS_THRESHOLD_MIN) {
  1039. pr_debug("%s: Too short, ignore button press\n", __func__);
  1040. goto done;
  1041. }
  1042. /* If switch interrupt already kicked in, ignore button press */
  1043. if (mbhc->in_swch_irq_handler) {
  1044. pr_debug("%s: Swtich level changed, ignore button press\n",
  1045. __func__);
  1046. goto done;
  1047. }
  1048. mask = wcd_mbhc_get_button_mask(mbhc);
  1049. if (mask == SND_JACK_BTN_0)
  1050. mbhc->btn_press_intr = true;
  1051. if (mbhc->current_plug != MBHC_PLUG_TYPE_HEADSET) {
  1052. pr_debug("%s: Plug isn't headset, ignore button press\n",
  1053. __func__);
  1054. goto done;
  1055. }
  1056. mbhc->buttons_pressed |= mask;
  1057. mbhc->mbhc_cb->lock_sleep(mbhc, true);
  1058. if (schedule_delayed_work(&mbhc->mbhc_btn_dwork,
  1059. msecs_to_jiffies(400)) == 0) {
  1060. WARN(1, "Button pressed twice without release event\n");
  1061. mbhc->mbhc_cb->lock_sleep(mbhc, false);
  1062. }
  1063. done:
  1064. pr_debug("%s: leave\n", __func__);
  1065. WCD_MBHC_RSC_UNLOCK(mbhc);
  1066. return IRQ_HANDLED;
  1067. }
  1068. static irqreturn_t wcd_mbhc_release_handler(int irq, void *data)
  1069. {
  1070. struct wcd_mbhc *mbhc = data;
  1071. int ret;
  1072. pr_debug("%s: enter\n", __func__);
  1073. WCD_MBHC_RSC_LOCK(mbhc);
  1074. if (wcd_swch_level_remove(mbhc)) {
  1075. pr_debug("%s: Switch level is low ", __func__);
  1076. goto exit;
  1077. }
  1078. if (mbhc->is_btn_press) {
  1079. mbhc->is_btn_press = false;
  1080. } else {
  1081. pr_debug("%s: This release is for fake btn press\n", __func__);
  1082. goto exit;
  1083. }
  1084. /*
  1085. * If current plug is headphone then there is no chance to
  1086. * get btn release interrupt, so connected cable should be
  1087. * headset not headphone.
  1088. * For ADC MBHC, ADC_COMPLETE interrupt will be generated
  1089. * in this case. So skip the check here.
  1090. */
  1091. if (mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY &&
  1092. mbhc->current_plug == MBHC_PLUG_TYPE_HEADPHONE) {
  1093. wcd_mbhc_find_plug_and_report(mbhc, MBHC_PLUG_TYPE_HEADSET);
  1094. goto exit;
  1095. }
  1096. if (mbhc->buttons_pressed & WCD_MBHC_JACK_BUTTON_MASK) {
  1097. ret = wcd_cancel_btn_work(mbhc);
  1098. if (ret == 0) {
  1099. pr_debug("%s: Reporting long button release event\n",
  1100. __func__);
  1101. wcd_mbhc_jack_report(mbhc, &mbhc->button_jack,
  1102. 0, mbhc->buttons_pressed);
  1103. } else {
  1104. if (mbhc->in_swch_irq_handler) {
  1105. pr_debug("%s: Switch irq kicked in, ignore\n",
  1106. __func__);
  1107. } else {
  1108. pr_debug("%s: Reporting btn press\n",
  1109. __func__);
  1110. wcd_mbhc_jack_report(mbhc,
  1111. &mbhc->button_jack,
  1112. mbhc->buttons_pressed,
  1113. mbhc->buttons_pressed);
  1114. pr_debug("%s: Reporting btn release\n",
  1115. __func__);
  1116. wcd_mbhc_jack_report(mbhc,
  1117. &mbhc->button_jack,
  1118. 0, mbhc->buttons_pressed);
  1119. }
  1120. }
  1121. mbhc->buttons_pressed &= ~WCD_MBHC_JACK_BUTTON_MASK;
  1122. }
  1123. exit:
  1124. pr_debug("%s: leave\n", __func__);
  1125. WCD_MBHC_RSC_UNLOCK(mbhc);
  1126. return IRQ_HANDLED;
  1127. }
  1128. static irqreturn_t wcd_mbhc_hphl_ocp_irq(int irq, void *data)
  1129. {
  1130. struct wcd_mbhc *mbhc = data;
  1131. int val;
  1132. pr_debug("%s: received HPHL OCP irq\n", __func__);
  1133. if (mbhc) {
  1134. if (mbhc->mbhc_cb->hph_register_recovery) {
  1135. if (mbhc->mbhc_cb->hph_register_recovery(mbhc)) {
  1136. WCD_MBHC_REG_READ(WCD_MBHC_HPHR_OCP_STATUS,
  1137. val);
  1138. if ((val != -EINVAL) && val)
  1139. mbhc->is_hph_ocp_pending = true;
  1140. goto done;
  1141. }
  1142. }
  1143. if (mbhc->hphlocp_cnt < OCP_ATTEMPT) {
  1144. mbhc->hphlocp_cnt++;
  1145. pr_debug("%s: retry, hphlocp_cnt: %d\n", __func__,
  1146. mbhc->hphlocp_cnt);
  1147. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1148. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1149. } else {
  1150. mbhc->mbhc_cb->irq_control(mbhc->codec,
  1151. mbhc->intr_ids->hph_left_ocp,
  1152. false);
  1153. mbhc->hph_status |= SND_JACK_OC_HPHL;
  1154. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1155. mbhc->hph_status,
  1156. WCD_MBHC_JACK_MASK);
  1157. }
  1158. } else {
  1159. pr_err("%s: Bad wcd9xxx_spmi private data\n", __func__);
  1160. }
  1161. done:
  1162. return IRQ_HANDLED;
  1163. }
  1164. static irqreturn_t wcd_mbhc_hphr_ocp_irq(int irq, void *data)
  1165. {
  1166. struct wcd_mbhc *mbhc = data;
  1167. pr_debug("%s: received HPHR OCP irq\n", __func__);
  1168. if (!mbhc) {
  1169. pr_err("%s: Bad mbhc private data\n", __func__);
  1170. goto done;
  1171. }
  1172. if (mbhc->is_hph_ocp_pending) {
  1173. mbhc->is_hph_ocp_pending = false;
  1174. goto done;
  1175. }
  1176. if (mbhc->mbhc_cb->hph_register_recovery) {
  1177. if (mbhc->mbhc_cb->hph_register_recovery(mbhc))
  1178. /* register corruption, hence reset registers */
  1179. goto done;
  1180. }
  1181. if (mbhc->hphrocp_cnt < OCP_ATTEMPT) {
  1182. mbhc->hphrocp_cnt++;
  1183. pr_debug("%s: retry, hphrocp_cnt: %d\n", __func__,
  1184. mbhc->hphrocp_cnt);
  1185. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 0);
  1186. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_OCP_FSM_EN, 1);
  1187. } else {
  1188. mbhc->mbhc_cb->irq_control(mbhc->codec,
  1189. mbhc->intr_ids->hph_right_ocp,
  1190. false);
  1191. mbhc->hph_status |= SND_JACK_OC_HPHR;
  1192. wcd_mbhc_jack_report(mbhc, &mbhc->headset_jack,
  1193. mbhc->hph_status, WCD_MBHC_JACK_MASK);
  1194. }
  1195. done:
  1196. return IRQ_HANDLED;
  1197. }
  1198. static int wcd_mbhc_initialise(struct wcd_mbhc *mbhc)
  1199. {
  1200. int ret = 0;
  1201. struct snd_soc_codec *codec = mbhc->codec;
  1202. pr_debug("%s: enter\n", __func__);
  1203. WCD_MBHC_RSC_LOCK(mbhc);
  1204. /* enable HS detection */
  1205. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1206. mbhc->mbhc_cb->hph_pull_up_control_v2(codec,
  1207. HS_PULLUP_I_DEFAULT);
  1208. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1209. mbhc->mbhc_cb->hph_pull_up_control(codec, I_DEFAULT);
  1210. else
  1211. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL, 3);
  1212. if (mbhc->mbhc_cfg->moisture_en && mbhc->mbhc_cb->mbhc_moisture_config
  1213. && !mbhc->mbhc_cfg->moisture_duty_cycle_en)
  1214. mbhc->mbhc_cb->mbhc_moisture_config(mbhc);
  1215. /*
  1216. * For USB analog we need to override the switch configuration.
  1217. * Also, disable hph_l pull-up current source as HS_DET_L is driven
  1218. * by an external source
  1219. */
  1220. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1221. mbhc->hphl_swh = 0;
  1222. mbhc->gnd_swh = 0;
  1223. if (mbhc->mbhc_cb->hph_pull_up_control_v2)
  1224. mbhc->mbhc_cb->hph_pull_up_control_v2(codec,
  1225. HS_PULLUP_I_OFF);
  1226. else if (mbhc->mbhc_cb->hph_pull_up_control)
  1227. mbhc->mbhc_cb->hph_pull_up_control(codec, I_OFF);
  1228. else
  1229. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_CTRL,
  1230. 0);
  1231. }
  1232. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HPHL_PLUG_TYPE, mbhc->hphl_swh);
  1233. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_GND_PLUG_TYPE, mbhc->gnd_swh);
  1234. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_SW_HPH_LP_100K_TO_GND, 1);
  1235. if (mbhc->mbhc_cfg->gnd_det_en && mbhc->mbhc_cb->mbhc_gnd_det_ctrl)
  1236. mbhc->mbhc_cb->mbhc_gnd_det_ctrl(codec, true);
  1237. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL, 1);
  1238. /*
  1239. * Disable L_DET for USB-C analog audio to avoid spurious interrupts
  1240. * when a non-audio accessory is inserted. L_DET_EN sets to 1 when FSA
  1241. * I2C driver notifies that ANALOG_AUDIO_ADAPTER is inserted
  1242. */
  1243. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1244. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 0);
  1245. else
  1246. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1247. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1248. /* Insertion debounce set to 48ms */
  1249. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 4);
  1250. } else {
  1251. /* Insertion debounce set to 96ms */
  1252. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_INSREM_DBNC, 6);
  1253. }
  1254. /* Button Debounce set to 16ms */
  1255. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_BTN_DBNC, 2);
  1256. /* Enable micbias ramp */
  1257. if (mbhc->mbhc_cb->mbhc_micb_ramp_control)
  1258. mbhc->mbhc_cb->mbhc_micb_ramp_control(codec, true);
  1259. /* enable bias */
  1260. mbhc->mbhc_cb->mbhc_bias(codec, true);
  1261. /* enable MBHC clock */
  1262. if (mbhc->mbhc_cb->clk_setup) {
  1263. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1264. mbhc->mbhc_cb->clk_setup(codec, false);
  1265. else
  1266. mbhc->mbhc_cb->clk_setup(codec, true);
  1267. }
  1268. /* program HS_VREF value */
  1269. wcd_program_hs_vref(mbhc);
  1270. wcd_program_btn_threshold(mbhc, false);
  1271. reinit_completion(&mbhc->btn_press_compl);
  1272. WCD_MBHC_RSC_UNLOCK(mbhc);
  1273. pr_debug("%s: leave\n", __func__);
  1274. return ret;
  1275. }
  1276. static void wcd_mbhc_fw_read(struct work_struct *work)
  1277. {
  1278. struct delayed_work *dwork;
  1279. struct wcd_mbhc *mbhc;
  1280. struct snd_soc_codec *codec;
  1281. const struct firmware *fw;
  1282. struct firmware_cal *fw_data = NULL;
  1283. int ret = -1, retry = 0;
  1284. bool use_default_cal = false;
  1285. dwork = to_delayed_work(work);
  1286. mbhc = container_of(dwork, struct wcd_mbhc, mbhc_firmware_dwork);
  1287. codec = mbhc->codec;
  1288. while (retry < FW_READ_ATTEMPTS) {
  1289. retry++;
  1290. pr_debug("%s:Attempt %d to request MBHC firmware\n",
  1291. __func__, retry);
  1292. if (mbhc->mbhc_cb->get_hwdep_fw_cal)
  1293. fw_data = mbhc->mbhc_cb->get_hwdep_fw_cal(mbhc,
  1294. WCD9XXX_MBHC_CAL);
  1295. if (!fw_data)
  1296. ret = request_firmware(&fw, "wcd9320/wcd9320_mbhc.bin",
  1297. codec->dev);
  1298. /*
  1299. * if request_firmware and hwdep cal both fail then
  1300. * sleep for 4sec for the userspace to send data to kernel
  1301. * retry for few times before bailing out
  1302. */
  1303. if ((ret != 0) && !fw_data) {
  1304. usleep_range(FW_READ_TIMEOUT, FW_READ_TIMEOUT +
  1305. WCD_MBHC_USLEEP_RANGE_MARGIN_US);
  1306. } else {
  1307. pr_debug("%s: MBHC Firmware read successful\n",
  1308. __func__);
  1309. break;
  1310. }
  1311. }
  1312. if (!fw_data)
  1313. pr_debug("%s: using request_firmware\n", __func__);
  1314. else
  1315. pr_debug("%s: using hwdep cal\n", __func__);
  1316. if (ret != 0 && !fw_data) {
  1317. pr_err("%s: Cannot load MBHC firmware use default cal\n",
  1318. __func__);
  1319. use_default_cal = true;
  1320. }
  1321. if (!use_default_cal) {
  1322. const void *data;
  1323. size_t size;
  1324. if (fw_data) {
  1325. data = fw_data->data;
  1326. size = fw_data->size;
  1327. } else {
  1328. data = fw->data;
  1329. size = fw->size;
  1330. }
  1331. if (wcd_mbhc_fw_validate(data, size) == false) {
  1332. pr_err("%s: Invalid MBHC cal data size use default cal\n",
  1333. __func__);
  1334. if (!fw_data)
  1335. release_firmware(fw);
  1336. } else {
  1337. if (fw_data) {
  1338. mbhc->mbhc_cfg->calibration =
  1339. (void *)fw_data->data;
  1340. mbhc->mbhc_cal = fw_data;
  1341. } else {
  1342. mbhc->mbhc_cfg->calibration =
  1343. (void *)fw->data;
  1344. mbhc->mbhc_fw = fw;
  1345. }
  1346. }
  1347. }
  1348. (void) wcd_mbhc_initialise(mbhc);
  1349. }
  1350. static int wcd_mbhc_set_keycode(struct wcd_mbhc *mbhc)
  1351. {
  1352. enum snd_jack_types type;
  1353. int i, ret, result = 0;
  1354. int *btn_key_code;
  1355. btn_key_code = mbhc->mbhc_cfg->key_code;
  1356. for (i = 0 ; i < WCD_MBHC_KEYCODE_NUM ; i++) {
  1357. if (btn_key_code[i] != 0) {
  1358. switch (i) {
  1359. case 0:
  1360. type = SND_JACK_BTN_0;
  1361. break;
  1362. case 1:
  1363. type = SND_JACK_BTN_1;
  1364. break;
  1365. case 2:
  1366. type = SND_JACK_BTN_2;
  1367. break;
  1368. case 3:
  1369. type = SND_JACK_BTN_3;
  1370. break;
  1371. case 4:
  1372. type = SND_JACK_BTN_4;
  1373. break;
  1374. case 5:
  1375. type = SND_JACK_BTN_5;
  1376. break;
  1377. default:
  1378. WARN_ONCE(1, "Wrong button number:%d\n", i);
  1379. result = -1;
  1380. return result;
  1381. }
  1382. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1383. type,
  1384. btn_key_code[i]);
  1385. if (ret) {
  1386. pr_err("%s: Failed to set code for %d\n",
  1387. __func__, btn_key_code[i]);
  1388. result = -1;
  1389. return result;
  1390. }
  1391. input_set_capability(
  1392. mbhc->button_jack.jack->input_dev,
  1393. EV_KEY, btn_key_code[i]);
  1394. pr_debug("%s: set btn%d key code:%d\n", __func__,
  1395. i, btn_key_code[i]);
  1396. }
  1397. }
  1398. if (btn_key_code[0])
  1399. mbhc->is_btn_already_regd = true;
  1400. return result;
  1401. }
  1402. static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
  1403. unsigned long mode, void *ptr)
  1404. {
  1405. struct wcd_mbhc *mbhc = container_of(nb, struct wcd_mbhc, fsa_nb);
  1406. if (!mbhc)
  1407. return -EINVAL;
  1408. dev_dbg(mbhc->codec->dev, "%s: mode = %lu\n", __func__, mode);
  1409. if (mode == POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER) {
  1410. if (mbhc->mbhc_cb->clk_setup)
  1411. mbhc->mbhc_cb->clk_setup(mbhc->codec, true);
  1412. /* insertion detected, enable L_DET_EN */
  1413. WCD_MBHC_REG_UPDATE_BITS(WCD_MBHC_L_DET_EN, 1);
  1414. }
  1415. return 0;
  1416. }
  1417. int wcd_mbhc_start(struct wcd_mbhc *mbhc, struct wcd_mbhc_config *mbhc_cfg)
  1418. {
  1419. int rc = 0;
  1420. struct snd_soc_codec *codec;
  1421. struct snd_soc_card *card;
  1422. const char *usb_c_dt = "qcom,msm-mbhc-usbc-audio-supported";
  1423. if (!mbhc || !mbhc_cfg)
  1424. return -EINVAL;
  1425. codec = mbhc->codec;
  1426. card = codec->component.card;
  1427. /* update the mbhc config */
  1428. mbhc->mbhc_cfg = mbhc_cfg;
  1429. dev_dbg(mbhc->codec->dev, "%s: enter\n", __func__);
  1430. /* check if USB C analog is defined on device tree */
  1431. mbhc_cfg->enable_usbc_analog = 0;
  1432. if (of_find_property(card->dev->of_node, usb_c_dt, NULL)) {
  1433. rc = of_property_read_u32(card->dev->of_node, usb_c_dt,
  1434. &mbhc_cfg->enable_usbc_analog);
  1435. }
  1436. if (mbhc_cfg->enable_usbc_analog == 0 || rc != 0) {
  1437. dev_dbg(card->dev,
  1438. "%s: %s in dt node is missing or false\n",
  1439. __func__, usb_c_dt);
  1440. dev_dbg(card->dev,
  1441. "%s: skipping USB c analog configuration\n", __func__);
  1442. }
  1443. /* Parse fsa switch handle */
  1444. if (mbhc_cfg->enable_usbc_analog) {
  1445. dev_dbg(mbhc->codec->dev, "%s: usbc analog enabled\n",
  1446. __func__);
  1447. mbhc->swap_thr = GND_MIC_USBC_SWAP_THRESHOLD;
  1448. mbhc->fsa_np = of_parse_phandle(card->dev->of_node,
  1449. "fsa4480-i2c-handle", 0);
  1450. if (!mbhc->fsa_np) {
  1451. dev_err(card->dev, "%s: fsa4480 i2c node not found\n",
  1452. __func__);
  1453. rc = -EINVAL;
  1454. goto err;
  1455. }
  1456. }
  1457. /* Set btn key code */
  1458. if ((!mbhc->is_btn_already_regd) && wcd_mbhc_set_keycode(mbhc))
  1459. pr_err("Set btn key code error!!!\n");
  1460. if (!mbhc->mbhc_cfg->read_fw_bin ||
  1461. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_fw) ||
  1462. (mbhc->mbhc_cfg->read_fw_bin && mbhc->mbhc_cal)) {
  1463. rc = wcd_mbhc_initialise(mbhc);
  1464. if (rc) {
  1465. dev_err(card->dev, "%s: wcd mbhc initialize failed\n",
  1466. __func__);
  1467. goto err;
  1468. }
  1469. } else {
  1470. if (!mbhc->mbhc_fw || !mbhc->mbhc_cal)
  1471. schedule_delayed_work(&mbhc->mbhc_firmware_dwork,
  1472. usecs_to_jiffies(FW_READ_TIMEOUT));
  1473. else
  1474. pr_err("%s: Skipping to read mbhc fw, 0x%pK %pK\n",
  1475. __func__, mbhc->mbhc_fw, mbhc->mbhc_cal);
  1476. }
  1477. if (mbhc_cfg->enable_usbc_analog) {
  1478. mbhc->fsa_nb.notifier_call = wcd_mbhc_usbc_ana_event_handler;
  1479. mbhc->fsa_nb.priority = 0;
  1480. rc = fsa4480_reg_notifier(&mbhc->fsa_nb, mbhc->fsa_np);
  1481. }
  1482. return rc;
  1483. err:
  1484. dev_dbg(mbhc->codec->dev, "%s: leave %d\n", __func__, rc);
  1485. return rc;
  1486. }
  1487. EXPORT_SYMBOL(wcd_mbhc_start);
  1488. void wcd_mbhc_stop(struct wcd_mbhc *mbhc)
  1489. {
  1490. pr_debug("%s: enter\n", __func__);
  1491. if (mbhc->current_plug != MBHC_PLUG_TYPE_NONE) {
  1492. if (mbhc->mbhc_cb && mbhc->mbhc_cb->skip_imped_detect)
  1493. mbhc->mbhc_cb->skip_imped_detect(mbhc->codec);
  1494. }
  1495. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1496. mbhc->hph_status = 0;
  1497. if (mbhc->mbhc_cb && mbhc->mbhc_cb->irq_control) {
  1498. mbhc->mbhc_cb->irq_control(mbhc->codec,
  1499. mbhc->intr_ids->hph_left_ocp,
  1500. false);
  1501. mbhc->mbhc_cb->irq_control(mbhc->codec,
  1502. mbhc->intr_ids->hph_right_ocp,
  1503. false);
  1504. }
  1505. if (mbhc->mbhc_fw || mbhc->mbhc_cal) {
  1506. cancel_delayed_work_sync(&mbhc->mbhc_firmware_dwork);
  1507. if (!mbhc->mbhc_cal)
  1508. release_firmware(mbhc->mbhc_fw);
  1509. mbhc->mbhc_fw = NULL;
  1510. mbhc->mbhc_cal = NULL;
  1511. }
  1512. if (mbhc->mbhc_cfg->enable_usbc_analog)
  1513. fsa4480_unreg_notifier(&mbhc->fsa_nb, mbhc->fsa_np);
  1514. pr_debug("%s: leave\n", __func__);
  1515. }
  1516. EXPORT_SYMBOL(wcd_mbhc_stop);
  1517. /*
  1518. * wcd_mbhc_init : initialize MBHC internal structures.
  1519. *
  1520. * NOTE: mbhc->mbhc_cfg is not YET configure so shouldn't be used
  1521. */
  1522. int wcd_mbhc_init(struct wcd_mbhc *mbhc, struct snd_soc_codec *codec,
  1523. const struct wcd_mbhc_cb *mbhc_cb,
  1524. const struct wcd_mbhc_intr *mbhc_cdc_intr_ids,
  1525. struct wcd_mbhc_register *wcd_mbhc_regs,
  1526. bool impedance_det_en)
  1527. {
  1528. int ret = 0;
  1529. int hph_swh = 0;
  1530. int gnd_swh = 0;
  1531. u32 hph_moist_config[3];
  1532. struct snd_soc_card *card = codec->component.card;
  1533. const char *hph_switch = "qcom,msm-mbhc-hphl-swh";
  1534. const char *gnd_switch = "qcom,msm-mbhc-gnd-swh";
  1535. const char *hs_thre = "qcom,msm-mbhc-hs-mic-max-threshold-mv";
  1536. const char *hph_thre = "qcom,msm-mbhc-hs-mic-min-threshold-mv";
  1537. pr_debug("%s: enter\n", __func__);
  1538. ret = of_property_read_u32(card->dev->of_node, hph_switch, &hph_swh);
  1539. if (ret) {
  1540. dev_err(card->dev,
  1541. "%s: missing %s in dt node\n", __func__, hph_switch);
  1542. goto err;
  1543. }
  1544. ret = of_property_read_u32(card->dev->of_node, gnd_switch, &gnd_swh);
  1545. if (ret) {
  1546. dev_err(card->dev,
  1547. "%s: missing %s in dt node\n", __func__, gnd_switch);
  1548. goto err;
  1549. }
  1550. ret = of_property_read_u32(card->dev->of_node, hs_thre,
  1551. &(mbhc->hs_thr));
  1552. if (ret)
  1553. dev_dbg(card->dev,
  1554. "%s: missing %s in dt node\n", __func__, hs_thre);
  1555. ret = of_property_read_u32(card->dev->of_node, hph_thre,
  1556. &(mbhc->hph_thr));
  1557. if (ret)
  1558. dev_dbg(card->dev,
  1559. "%s: missing %s in dt node\n", __func__, hph_thre);
  1560. ret = of_property_read_u32_array(card->dev->of_node,
  1561. "qcom,msm-mbhc-moist-cfg",
  1562. hph_moist_config, 3);
  1563. if (ret) {
  1564. dev_dbg(card->dev, "%s: no qcom,msm-mbhc-moist-cfg in DT\n",
  1565. __func__);
  1566. mbhc->moist_vref = V_45_MV;
  1567. mbhc->moist_iref = I_3P0_UA;
  1568. mbhc->moist_rref = R_24_KOHM;
  1569. } else {
  1570. mbhc->moist_vref = hph_moist_config[0];
  1571. mbhc->moist_iref = hph_moist_config[1];
  1572. mbhc->moist_rref = hph_moist_config[2];
  1573. }
  1574. mbhc->in_swch_irq_handler = false;
  1575. mbhc->current_plug = MBHC_PLUG_TYPE_NONE;
  1576. mbhc->is_btn_press = false;
  1577. mbhc->codec = codec;
  1578. mbhc->intr_ids = mbhc_cdc_intr_ids;
  1579. mbhc->impedance_detect = impedance_det_en;
  1580. mbhc->hphl_swh = hph_swh;
  1581. mbhc->gnd_swh = gnd_swh;
  1582. mbhc->micbias_enable = false;
  1583. mbhc->mbhc_cb = mbhc_cb;
  1584. mbhc->btn_press_intr = false;
  1585. mbhc->is_hs_recording = false;
  1586. mbhc->is_extn_cable = false;
  1587. mbhc->extn_cable_hph_rem = false;
  1588. mbhc->hph_type = WCD_MBHC_HPH_NONE;
  1589. mbhc->wcd_mbhc_regs = wcd_mbhc_regs;
  1590. mbhc->swap_thr = GND_MIC_SWAP_THRESHOLD;
  1591. if (mbhc->intr_ids == NULL) {
  1592. pr_err("%s: Interrupt mapping not provided\n", __func__);
  1593. return -EINVAL;
  1594. }
  1595. if (!mbhc->wcd_mbhc_regs) {
  1596. dev_err(codec->dev, "%s: mbhc registers are not defined\n",
  1597. __func__);
  1598. return -EINVAL;
  1599. }
  1600. /* Check if IRQ and other required callbacks are defined or not */
  1601. if (!mbhc_cb || !mbhc_cb->request_irq || !mbhc_cb->irq_control ||
  1602. !mbhc_cb->free_irq || !mbhc_cb->map_btn_code_to_num ||
  1603. !mbhc_cb->lock_sleep || !mbhc_cb->mbhc_bias ||
  1604. !mbhc_cb->set_btn_thr) {
  1605. dev_err(codec->dev, "%s: required mbhc callbacks are not defined\n",
  1606. __func__);
  1607. return -EINVAL;
  1608. }
  1609. /* No need to create new sound card jacks if is is already created */
  1610. if (mbhc->headset_jack.jack == NULL) {
  1611. ret = snd_soc_card_jack_new(codec->component.card,
  1612. "Headset Jack", WCD_MBHC_JACK_MASK,
  1613. &mbhc->headset_jack, NULL, 0);
  1614. if (ret) {
  1615. pr_err("%s: Failed to create new jack\n", __func__);
  1616. return ret;
  1617. }
  1618. ret = snd_soc_card_jack_new(codec->component.card,
  1619. "Button Jack",
  1620. WCD_MBHC_JACK_BUTTON_MASK,
  1621. &mbhc->button_jack, NULL, 0);
  1622. if (ret) {
  1623. pr_err("Failed to create new jack\n");
  1624. return ret;
  1625. }
  1626. ret = snd_jack_set_key(mbhc->button_jack.jack,
  1627. SND_JACK_BTN_0,
  1628. KEY_MEDIA);
  1629. if (ret) {
  1630. pr_err("%s: Failed to set code for btn-0\n",
  1631. __func__);
  1632. return ret;
  1633. }
  1634. INIT_DELAYED_WORK(&mbhc->mbhc_firmware_dwork,
  1635. wcd_mbhc_fw_read);
  1636. INIT_DELAYED_WORK(&mbhc->mbhc_btn_dwork, wcd_btn_lpress_fn);
  1637. }
  1638. mutex_init(&mbhc->hphl_pa_lock);
  1639. mutex_init(&mbhc->hphr_pa_lock);
  1640. init_completion(&mbhc->btn_press_compl);
  1641. /* Register event notifier */
  1642. mbhc->nblock.notifier_call = wcd_event_notify;
  1643. if (mbhc->mbhc_cb->register_notifier) {
  1644. ret = mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock,
  1645. true);
  1646. if (ret) {
  1647. pr_err("%s: Failed to register notifier %d\n",
  1648. __func__, ret);
  1649. return ret;
  1650. }
  1651. }
  1652. init_waitqueue_head(&mbhc->wait_btn_press);
  1653. mutex_init(&mbhc->codec_resource_lock);
  1654. switch (mbhc->mbhc_detection_logic) {
  1655. case WCD_DETECTION_LEGACY:
  1656. wcd_mbhc_legacy_init(mbhc);
  1657. break;
  1658. case WCD_DETECTION_ADC:
  1659. wcd_mbhc_adc_init(mbhc);
  1660. break;
  1661. default:
  1662. pr_err("%s: Unknown detection logic type %d\n",
  1663. __func__, mbhc->mbhc_detection_logic);
  1664. break;
  1665. }
  1666. if (!mbhc->mbhc_fn ||
  1667. !mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq ||
  1668. !mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq ||
  1669. !mbhc->mbhc_fn->wcd_mbhc_detect_plug_type ||
  1670. !mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1671. pr_err("%s: mbhc function pointer is NULL\n", __func__);
  1672. goto err_mbhc_sw_irq;
  1673. }
  1674. ret = mbhc->mbhc_cb->request_irq(codec, mbhc->intr_ids->mbhc_sw_intr,
  1675. wcd_mbhc_mech_plug_detect_irq,
  1676. "mbhc sw intr", mbhc);
  1677. if (ret) {
  1678. pr_err("%s: Failed to request irq %d, ret = %d\n", __func__,
  1679. mbhc->intr_ids->mbhc_sw_intr, ret);
  1680. goto err_mbhc_sw_irq;
  1681. }
  1682. ret = mbhc->mbhc_cb->request_irq(codec,
  1683. mbhc->intr_ids->mbhc_btn_press_intr,
  1684. wcd_mbhc_btn_press_handler,
  1685. "Button Press detect", mbhc);
  1686. if (ret) {
  1687. pr_err("%s: Failed to request irq %d\n", __func__,
  1688. mbhc->intr_ids->mbhc_btn_press_intr);
  1689. goto err_btn_press_irq;
  1690. }
  1691. ret = mbhc->mbhc_cb->request_irq(codec,
  1692. mbhc->intr_ids->mbhc_btn_release_intr,
  1693. wcd_mbhc_release_handler,
  1694. "Button Release detect", mbhc);
  1695. if (ret) {
  1696. pr_err("%s: Failed to request irq %d\n", __func__,
  1697. mbhc->intr_ids->mbhc_btn_release_intr);
  1698. goto err_btn_release_irq;
  1699. }
  1700. ret = mbhc->mbhc_cb->request_irq(codec,
  1701. mbhc->intr_ids->mbhc_hs_ins_intr,
  1702. mbhc->mbhc_fn->wcd_mbhc_hs_ins_irq,
  1703. "Elect Insert", mbhc);
  1704. if (ret) {
  1705. pr_err("%s: Failed to request irq %d\n", __func__,
  1706. mbhc->intr_ids->mbhc_hs_ins_intr);
  1707. goto err_mbhc_hs_ins_irq;
  1708. }
  1709. mbhc->mbhc_cb->irq_control(codec, mbhc->intr_ids->mbhc_hs_ins_intr,
  1710. false);
  1711. clear_bit(WCD_MBHC_ELEC_HS_INS, &mbhc->intr_status);
  1712. ret = mbhc->mbhc_cb->request_irq(codec,
  1713. mbhc->intr_ids->mbhc_hs_rem_intr,
  1714. mbhc->mbhc_fn->wcd_mbhc_hs_rem_irq,
  1715. "Elect Remove", mbhc);
  1716. if (ret) {
  1717. pr_err("%s: Failed to request irq %d\n", __func__,
  1718. mbhc->intr_ids->mbhc_hs_rem_intr);
  1719. goto err_mbhc_hs_rem_irq;
  1720. }
  1721. mbhc->mbhc_cb->irq_control(codec, mbhc->intr_ids->mbhc_hs_rem_intr,
  1722. false);
  1723. clear_bit(WCD_MBHC_ELEC_HS_REM, &mbhc->intr_status);
  1724. ret = mbhc->mbhc_cb->request_irq(codec, mbhc->intr_ids->hph_left_ocp,
  1725. wcd_mbhc_hphl_ocp_irq, "HPH_L OCP detect",
  1726. mbhc);
  1727. if (ret) {
  1728. pr_err("%s: Failed to request irq %d\n", __func__,
  1729. mbhc->intr_ids->hph_left_ocp);
  1730. goto err_hphl_ocp_irq;
  1731. }
  1732. ret = mbhc->mbhc_cb->request_irq(codec, mbhc->intr_ids->hph_right_ocp,
  1733. wcd_mbhc_hphr_ocp_irq, "HPH_R OCP detect",
  1734. mbhc);
  1735. if (ret) {
  1736. pr_err("%s: Failed to request irq %d\n", __func__,
  1737. mbhc->intr_ids->hph_right_ocp);
  1738. goto err_hphr_ocp_irq;
  1739. }
  1740. mbhc->deinit_in_progress = false;
  1741. pr_debug("%s: leave ret %d\n", __func__, ret);
  1742. return ret;
  1743. err_hphr_ocp_irq:
  1744. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->hph_left_ocp, mbhc);
  1745. err_hphl_ocp_irq:
  1746. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_hs_rem_intr, mbhc);
  1747. err_mbhc_hs_rem_irq:
  1748. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_hs_ins_intr, mbhc);
  1749. err_mbhc_hs_ins_irq:
  1750. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_btn_release_intr,
  1751. mbhc);
  1752. err_btn_release_irq:
  1753. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_btn_press_intr,
  1754. mbhc);
  1755. err_btn_press_irq:
  1756. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1757. err_mbhc_sw_irq:
  1758. if (mbhc->mbhc_cb->register_notifier)
  1759. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1760. mutex_destroy(&mbhc->codec_resource_lock);
  1761. err:
  1762. pr_debug("%s: leave ret %d\n", __func__, ret);
  1763. return ret;
  1764. }
  1765. EXPORT_SYMBOL(wcd_mbhc_init);
  1766. void wcd_mbhc_deinit(struct wcd_mbhc *mbhc)
  1767. {
  1768. struct snd_soc_codec *codec = mbhc->codec;
  1769. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_sw_intr, mbhc);
  1770. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_btn_press_intr,
  1771. mbhc);
  1772. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_btn_release_intr,
  1773. mbhc);
  1774. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_hs_ins_intr, mbhc);
  1775. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->mbhc_hs_rem_intr, mbhc);
  1776. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->hph_left_ocp, mbhc);
  1777. mbhc->mbhc_cb->free_irq(codec, mbhc->intr_ids->hph_right_ocp, mbhc);
  1778. if (mbhc->mbhc_cb && mbhc->mbhc_cb->register_notifier)
  1779. mbhc->mbhc_cb->register_notifier(mbhc, &mbhc->nblock, false);
  1780. if (mbhc->mbhc_fn->wcd_cancel_hs_detect_plug) {
  1781. WCD_MBHC_RSC_LOCK(mbhc);
  1782. mbhc->mbhc_fn->wcd_cancel_hs_detect_plug(mbhc,
  1783. &mbhc->correct_plug_swch);
  1784. WCD_MBHC_RSC_UNLOCK(mbhc);
  1785. }
  1786. mutex_destroy(&mbhc->codec_resource_lock);
  1787. mutex_destroy(&mbhc->hphl_pa_lock);
  1788. mutex_destroy(&mbhc->hphr_pa_lock);
  1789. }
  1790. EXPORT_SYMBOL(wcd_mbhc_deinit);
  1791. static int __init mbhc_init(void)
  1792. {
  1793. return 0;
  1794. }
  1795. static void __exit mbhc_exit(void)
  1796. {
  1797. }
  1798. module_init(mbhc_init);
  1799. module_exit(mbhc_exit);
  1800. MODULE_DESCRIPTION("wcd MBHC v2 module");
  1801. MODULE_LICENSE("GPL v2");