wcd-mbhc-v2.c 61 KB

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