wcd-mbhc-v2.c 64 KB

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