wcd9378-mbhc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.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/kernel.h>
  13. #include <linux/gpio.h>
  14. #include <linux/delay.h>
  15. #include <linux/regmap.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include <sound/soc-dapm.h>
  20. #include <asoc/wcdcal-hwdep.h>
  21. #include <asoc/wcd-mbhc-v2-api.h>
  22. #include "wcd9378-registers.h"
  23. #include "internal.h"
  24. #define WCD9378_ZDET_SUPPORTED true
  25. /* Z value defined in milliohm */
  26. #define WCD9378_ZDET_VAL_32 32000
  27. #define WCD9378_ZDET_VAL_400 400000
  28. #define WCD9378_ZDET_VAL_1200 1200000
  29. #define WCD9378_ZDET_VAL_100K 100000000
  30. /* Z floating defined in ohms */
  31. #define WCD9378_ZDET_FLOATING_IMPEDANCE 0x0FFFFFFE
  32. #define WCD9378_ZDET_NUM_MEASUREMENTS 900
  33. #define WCD9378_MBHC_GET_C1(c) ((c & 0xC000) >> 14)
  34. #define WCD9378_MBHC_GET_X1(x) (x & 0x3FFF)
  35. /* Z value compared in milliOhm */
  36. #define WCD9378_MBHC_IS_SECOND_RAMP_REQUIRED(z) ((z > 400000) || (z < 32000))
  37. #define WCD9378_MBHC_ZDET_CONST (86 * 16384)
  38. #define WCD9378_MBHC_MOISTURE_RREF R_24_KOHM
  39. static struct wcd_mbhc_register
  40. wcd_mbhc_registers[WCD_MBHC_REG_FUNC_MAX] = {
  41. WCD_MBHC_REGISTER("WCD_MBHC_L_DET_EN",
  42. WCD9378_ANA_MBHC_MECH, 0x80, 7, 0),
  43. WCD_MBHC_REGISTER("WCD_MBHC_GND_DET_EN",
  44. WCD9378_ANA_MBHC_MECH, 0x40, 6, 0),
  45. WCD_MBHC_REGISTER("WCD_MBHC_MECH_DETECTION_TYPE",
  46. WCD9378_ANA_MBHC_MECH, 0x20, 5, 0),
  47. WCD_MBHC_REGISTER("WCD_MBHC_MIC_CLAMP_CTL",
  48. WCD9378_MBHC_NEW_PLUG_DETECT_CTL, 0x30, 4, 0),
  49. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_DETECTION_TYPE",
  50. WCD9378_ANA_MBHC_ELECT, 0x08, 3, 0),
  51. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_CTRL",
  52. WCD9378_MBHC_NEW_INT_MECH_DET_CURRENT, 0x1F, 0, 0),
  53. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL",
  54. WCD9378_ANA_MBHC_MECH, 0x04, 2, 0),
  55. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PLUG_TYPE",
  56. WCD9378_ANA_MBHC_MECH, 0x10, 4, 0),
  57. WCD_MBHC_REGISTER("WCD_MBHC_GND_PLUG_TYPE",
  58. WCD9378_ANA_MBHC_MECH, 0x08, 3, 0),
  59. WCD_MBHC_REGISTER("WCD_MBHC_SW_HPH_LP_100K_TO_GND",
  60. WCD9378_ANA_MBHC_MECH, 0x01, 0, 0),
  61. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_SCHMT_ISRC",
  62. WCD9378_ANA_MBHC_ELECT, 0x06, 1, 0),
  63. WCD_MBHC_REGISTER("WCD_MBHC_FSM_EN",
  64. WCD9378_ANA_MBHC_ELECT, 0x80, 7, 0),
  65. WCD_MBHC_REGISTER("WCD_MBHC_INSREM_DBNC",
  66. WCD9378_MBHC_NEW_PLUG_DETECT_CTL, 0x0F, 0, 0),
  67. WCD_MBHC_REGISTER("WCD_MBHC_BTN_DBNC",
  68. WCD9378_MBHC_NEW_CTL_1, 0x0F, 0, 0),
  69. WCD_MBHC_REGISTER("WCD_MBHC_HS_VREF",
  70. WCD9378_MBHC_NEW_CTL_2, 0x03, 0, 0),
  71. WCD_MBHC_REGISTER("WCD_MBHC_HS_COMP_RESULT",
  72. WCD9378_ANA_MBHC_RESULT_3, 0x08, 3, 0),
  73. WCD_MBHC_REGISTER("WCD_MBHC_IN2P_CLAMP_STATE",
  74. WCD9378_ANA_MBHC_RESULT_3, 0x10, 4, 0),
  75. WCD_MBHC_REGISTER("WCD_MBHC_MIC_SCHMT_RESULT",
  76. WCD9378_ANA_MBHC_RESULT_3, 0x20, 5, 0),
  77. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_SCHMT_RESULT",
  78. WCD9378_ANA_MBHC_RESULT_3, 0x80, 7, 0),
  79. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_SCHMT_RESULT",
  80. WCD9378_ANA_MBHC_RESULT_3, 0x40, 6, 0),
  81. WCD_MBHC_REGISTER("WCD_MBHC_OCP_FSM_EN",
  82. WCD9378_HPH_OCP_CTL, 0x10, 4, 0),
  83. WCD_MBHC_REGISTER("WCD_MBHC_BTN_RESULT",
  84. WCD9378_ANA_MBHC_RESULT_3, 0x07, 0, 0),
  85. WCD_MBHC_REGISTER("WCD_MBHC_BTN_ISRC_CTL",
  86. WCD9378_ANA_MBHC_ELECT, 0x70, 4, 0),
  87. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_RESULT",
  88. WCD9378_ANA_MBHC_RESULT_3, 0xFF, 0, 0),
  89. WCD_MBHC_REGISTER("WCD_MBHC_MICB_CTRL",
  90. WCD9378_ANA_MICB2, 0xC0, 6, 0),
  91. WCD_MBHC_REGISTER("WCD_MBHC_HPH_CNP_WG_TIME",
  92. WCD9378_HPH_CNP_WG_TIME, 0xFF, 0, 0),
  93. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_PA_EN",
  94. WCD9378_ANA_HPH, 0x40, 6, 0),
  95. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PA_EN",
  96. WCD9378_ANA_HPH, 0x80, 7, 0),
  97. WCD_MBHC_REGISTER("WCD_MBHC_HPH_PA_EN",
  98. WCD9378_ANA_HPH, 0xC0, 6, 0),
  99. WCD_MBHC_REGISTER("WCD_MBHC_SWCH_LEVEL_REMOVE",
  100. WCD9378_ANA_MBHC_RESULT_3, 0x10, 4, 0),
  101. WCD_MBHC_REGISTER("WCD_MBHC_PULLDOWN_CTRL",
  102. 0, 0, 0, 0),
  103. WCD_MBHC_REGISTER("WCD_MBHC_ANC_DET_EN",
  104. WCD9378_MBHC_CTL_BCS, 0x02, 1, 0),
  105. WCD_MBHC_REGISTER("WCD_MBHC_FSM_STATUS",
  106. WCD9378_MBHC_NEW_FSM_STATUS, 0x01, 0, 0),
  107. WCD_MBHC_REGISTER("WCD_MBHC_MUX_CTL",
  108. WCD9378_MBHC_NEW_CTL_2, 0x70, 4, 0),
  109. WCD_MBHC_REGISTER("WCD_MBHC_MOISTURE_STATUS",
  110. WCD9378_MBHC_NEW_FSM_STATUS, 0x20, 5, 0),
  111. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_GND",
  112. WCD9378_HPH_PA_CTL2, 0x40, 6, 0),
  113. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_GND",
  114. WCD9378_HPH_PA_CTL2, 0x10, 4, 0),
  115. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_DET_EN",
  116. WCD9378_HPH_L_TEST, 0x01, 0, 0),
  117. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_DET_EN",
  118. WCD9378_HPH_R_TEST, 0x01, 0, 0),
  119. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_STATUS",
  120. SWRS_SCP_SDCA_INTSTAT_1, 0x80, 7, 0),
  121. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_STATUS",
  122. SWRS_SCP_SDCA_INTSTAT_1, 0x20, 5, 0),
  123. WCD_MBHC_REGISTER("WCD_MBHC_ADC_EN",
  124. WCD9378_MBHC_NEW_CTL_1, 0x40, 6, 0),
  125. WCD_MBHC_REGISTER("WCD_MBHC_ADC_COMPLETE", WCD9378_MBHC_NEW_FSM_STATUS,
  126. 0x40, 6, 0),
  127. WCD_MBHC_REGISTER("WCD_MBHC_ADC_TIMEOUT", WCD9378_MBHC_NEW_FSM_STATUS,
  128. 0x80, 7, 0),
  129. WCD_MBHC_REGISTER("WCD_MBHC_ADC_RESULT", WCD9378_MBHC_NEW_ADC_RESULT,
  130. 0xFF, 0, 0),
  131. WCD_MBHC_REGISTER("WCD_MBHC_MICB2_VOUT", WCD9378_ANA_MICB2, 0x3F, 0, 0),
  132. WCD_MBHC_REGISTER("WCD_MBHC_ADC_MODE",
  133. WCD9378_MBHC_NEW_CTL_1, 0x10, 4, 0),
  134. WCD_MBHC_REGISTER("WCD_MBHC_DETECTION_DONE",
  135. WCD9378_MBHC_NEW_CTL_1, 0x20, 5, 0),
  136. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_ISRC_EN",
  137. WCD9378_ANA_MBHC_ZDET, 0x02, 1, 0),
  138. };
  139. static const struct wcd_mbhc_intr intr_ids = {
  140. .mbhc_sw_intr = WCD9378_IRQ_MBHC_SW_DET,
  141. .mbhc_btn_press_intr = WCD9378_IRQ_MBHC_BUTTON_PRESS_DET,
  142. .mbhc_btn_release_intr = WCD9378_IRQ_MBHC_BUTTON_RELEASE_DET,
  143. .mbhc_hs_ins_intr = WCD9378_IRQ_MBHC_ELECT_INS_REM_LEG_DET,
  144. .mbhc_hs_rem_intr = WCD9378_IRQ_MBHC_ELECT_INS_REM_DET,
  145. .hph_left_ocp = WCD9378_IRQ_HPHL_OCP_INT,
  146. .hph_right_ocp = WCD9378_IRQ_HPHR_OCP_INT,
  147. };
  148. struct wcd9378_mbhc_zdet_param {
  149. u16 ldo_ctl;
  150. u16 noff;
  151. u16 nshift;
  152. u16 btn5;
  153. u16 btn6;
  154. u16 btn7;
  155. };
  156. static int wcd9378_mbhc_request_irq(struct snd_soc_component *component,
  157. int irq, irq_handler_t handler,
  158. const char *name, void *data)
  159. {
  160. struct wcd9378_priv *wcd9378 = dev_get_drvdata(component->dev);
  161. return wcd_request_irq(&wcd9378->irq_info, irq, name, handler, data);
  162. }
  163. static void wcd9378_mbhc_irq_control(struct snd_soc_component *component,
  164. int irq, bool enable)
  165. {
  166. struct wcd9378_priv *wcd9378 = dev_get_drvdata(component->dev);
  167. if (enable)
  168. wcd_enable_irq(&wcd9378->irq_info, irq);
  169. else
  170. wcd_disable_irq(&wcd9378->irq_info, irq);
  171. }
  172. static int wcd9378_mbhc_free_irq(struct snd_soc_component *component,
  173. int irq, void *data)
  174. {
  175. struct wcd9378_priv *wcd9378 = dev_get_drvdata(component->dev);
  176. wcd_free_irq(&wcd9378->irq_info, irq, data);
  177. return 0;
  178. }
  179. static void wcd9378_mbhc_clk_setup(struct snd_soc_component *component,
  180. bool enable)
  181. {
  182. if (enable) {
  183. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_1,
  184. 0x80, 0x80);
  185. snd_soc_component_update_bits(component, WCD9378_CDC_ANA_TX_CLK_CTL,
  186. 0x01, 0x01);
  187. } else {
  188. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_1,
  189. 0x80, 0x00);
  190. snd_soc_component_update_bits(component, WCD9378_CDC_ANA_TX_CLK_CTL,
  191. 0x01, 0x00);
  192. }
  193. }
  194. static int wcd9378_mbhc_btn_to_num(struct snd_soc_component *component)
  195. {
  196. return snd_soc_component_read(component, WCD9378_ANA_MBHC_RESULT_3) & 0x7;
  197. }
  198. static void wcd9378_mbhc_mbhc_bias_control(struct snd_soc_component *component,
  199. bool enable)
  200. {
  201. if (enable)
  202. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_ELECT,
  203. 0x01, 0x01);
  204. else
  205. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_ELECT,
  206. 0x01, 0x00);
  207. }
  208. static void wcd9378_mbhc_program_btn_thr(struct snd_soc_component *component,
  209. s16 *btn_low, s16 *btn_high,
  210. int num_btn, bool is_micbias)
  211. {
  212. int i;
  213. int vth;
  214. if (num_btn > WCD_MBHC_DEF_BUTTONS) {
  215. dev_err(component->dev, "%s: invalid number of buttons: %d\n",
  216. __func__, num_btn);
  217. return;
  218. }
  219. for (i = 0; i < num_btn; i++) {
  220. vth = ((btn_high[i] * 2) / 25) & 0x3F;
  221. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_BTN0 + i,
  222. 0xFC, vth << 2);
  223. dev_dbg(component->dev, "%s: btn_high[%d]: %d, vth: %d\n",
  224. __func__, i, btn_high[i], vth);
  225. }
  226. }
  227. static bool wcd9378_mbhc_lock_sleep(struct wcd_mbhc *mbhc, bool lock)
  228. {
  229. struct snd_soc_component *component = mbhc->component;
  230. struct wcd9378_priv *wcd9378 = dev_get_drvdata(component->dev);
  231. wcd9378->wakeup((void *)wcd9378, lock);
  232. return true;
  233. }
  234. static int wcd9378_mbhc_register_notifier(struct wcd_mbhc *mbhc,
  235. struct notifier_block *nblock,
  236. bool enable)
  237. {
  238. struct wcd9378_mbhc *wcd9378_mbhc;
  239. wcd9378_mbhc = container_of(mbhc, struct wcd9378_mbhc, wcd_mbhc);
  240. if (enable)
  241. return blocking_notifier_chain_register(&wcd9378_mbhc->notifier,
  242. nblock);
  243. else
  244. return blocking_notifier_chain_unregister(
  245. &wcd9378_mbhc->notifier, nblock);
  246. }
  247. static bool wcd9378_mbhc_micb_en_status(struct wcd_mbhc *mbhc, int micb_num)
  248. {
  249. u8 val = 0;
  250. if (micb_num == MIC_BIAS_2) {
  251. val = ((snd_soc_component_read(mbhc->component,
  252. WCD9378_ANA_MICB2) & 0xC0)
  253. >> 6);
  254. if (val == 0x01)
  255. return true;
  256. }
  257. return false;
  258. }
  259. static bool wcd9378_mbhc_hph_pa_on_status(struct snd_soc_component *component)
  260. {
  261. return (snd_soc_component_read(component, WCD9378_ANA_HPH) & 0xC0) ?
  262. true : false;
  263. }
  264. static void wcd9378_mbhc_hph_l_pull_up_control(
  265. struct snd_soc_component *component,
  266. int pull_up_cur)
  267. {
  268. /* Default pull up current to 2uA */
  269. if (pull_up_cur > HS_PULLUP_I_OFF || pull_up_cur < HS_PULLUP_I_3P0_UA ||
  270. pull_up_cur == HS_PULLUP_I_DEFAULT)
  271. pull_up_cur = HS_PULLUP_I_2P0_UA;
  272. dev_dbg(component->dev, "%s: HS pull up current:%d\n",
  273. __func__, pull_up_cur);
  274. snd_soc_component_update_bits(component,
  275. WCD9378_MBHC_NEW_INT_MECH_DET_CURRENT,
  276. 0x1F, pull_up_cur);
  277. }
  278. static int wcd9378_mbhc_request_micbias(struct snd_soc_component *component,
  279. int micb_num, int req)
  280. {
  281. int ret = 0;
  282. ret = wcd9378_micbias_control(component, micb_num, req, false);
  283. return ret;
  284. }
  285. static void wcd9378_mbhc_micb_ramp_control(struct snd_soc_component *component,
  286. bool enable)
  287. {
  288. if (enable) {
  289. snd_soc_component_update_bits(component, WCD9378_ANA_MICB2_RAMP,
  290. 0x1C, 0x0C);
  291. snd_soc_component_update_bits(component, WCD9378_ANA_MICB2_RAMP,
  292. 0x80, 0x80);
  293. } else {
  294. snd_soc_component_update_bits(component, WCD9378_ANA_MICB2_RAMP,
  295. 0x80, 0x00);
  296. snd_soc_component_update_bits(component, WCD9378_ANA_MICB2_RAMP,
  297. 0x1C, 0x00);
  298. }
  299. }
  300. static struct firmware_cal *wcd9378_get_hwdep_fw_cal(struct wcd_mbhc *mbhc,
  301. enum wcd_cal_type type)
  302. {
  303. struct wcd9378_mbhc *wcd9378_mbhc;
  304. struct firmware_cal *hwdep_cal;
  305. struct snd_soc_component *component = mbhc->component;
  306. wcd9378_mbhc = container_of(mbhc, struct wcd9378_mbhc, wcd_mbhc);
  307. if (!component) {
  308. pr_err("%s: NULL component pointer\n", __func__);
  309. return NULL;
  310. }
  311. hwdep_cal = wcdcal_get_fw_cal(wcd9378_mbhc->fw_data, type);
  312. if (!hwdep_cal)
  313. dev_err(component->dev, "%s: cal not sent by %d\n",
  314. __func__, type);
  315. return hwdep_cal;
  316. }
  317. static int wcd9378_mbhc_micb_ctrl_threshold_mic(
  318. struct snd_soc_component *component,
  319. int micb_num, bool req_en)
  320. {
  321. struct wcd9378_pdata *pdata = dev_get_platdata(component->dev);
  322. int rc, micb_mv;
  323. if (micb_num != MIC_BIAS_2)
  324. return -EINVAL;
  325. /*
  326. * If device tree micbias level is already above the minimum
  327. * voltage needed to detect threshold microphone, then do
  328. * not change the micbias, just return.
  329. */
  330. if (pdata->micbias.micb2_mv >= WCD_MBHC_THR_HS_MICB_MV)
  331. return 0;
  332. micb_mv = req_en ? WCD_MBHC_THR_HS_MICB_MV : pdata->micbias.micb2_mv;
  333. rc = wcd9378_mbhc_micb_adjust_voltage(component, micb_mv, MIC_BIAS_2);
  334. return rc;
  335. }
  336. static inline void wcd9378_mbhc_get_result_params(struct wcd9378_priv *wcd9378,
  337. s16 *d1_a, u16 noff,
  338. int32_t *zdet)
  339. {
  340. int i;
  341. int val, val1;
  342. s16 c1;
  343. s32 x1, d1;
  344. int32_t denom;
  345. int minCode_param[] = {
  346. 3277, 1639, 820, 410, 205, 103, 52, 26
  347. };
  348. regmap_update_bits(wcd9378->regmap, WCD9378_ANA_MBHC_ZDET, 0x20, 0x20);
  349. for (i = 0; i < WCD9378_ZDET_NUM_MEASUREMENTS; i++) {
  350. regmap_read(wcd9378->regmap, WCD9378_ANA_MBHC_RESULT_2, &val);
  351. if (val & 0x80)
  352. break;
  353. }
  354. val = val << 0x8;
  355. regmap_read(wcd9378->regmap, WCD9378_ANA_MBHC_RESULT_1, &val1);
  356. val |= val1;
  357. regmap_update_bits(wcd9378->regmap, WCD9378_ANA_MBHC_ZDET, 0x20, 0x00);
  358. x1 = WCD9378_MBHC_GET_X1(val);
  359. c1 = WCD9378_MBHC_GET_C1(val);
  360. /* If ramp is not complete, give additional 5ms */
  361. if ((c1 < 2) && x1)
  362. usleep_range(5000, 5050);
  363. if (!c1 || !x1) {
  364. dev_dbg(wcd9378->dev,
  365. "%s: Impedance detect ramp error, c1=%d, x1=0x%x\n",
  366. __func__, c1, x1);
  367. goto ramp_down;
  368. }
  369. d1 = d1_a[c1];
  370. denom = (x1 * d1) - (1 << (14 - noff));
  371. if (denom > 0)
  372. *zdet = (WCD9378_MBHC_ZDET_CONST * 1000) / denom;
  373. else if (x1 < minCode_param[noff])
  374. *zdet = WCD9378_ZDET_FLOATING_IMPEDANCE;
  375. dev_dbg(wcd9378->dev, "%s: d1=%d, c1=%d, x1=0x%x, z_val=%d(milliOhm)\n",
  376. __func__, d1, c1, x1, *zdet);
  377. ramp_down:
  378. i = 0;
  379. while (x1) {
  380. regmap_read(wcd9378->regmap,
  381. WCD9378_ANA_MBHC_RESULT_1, &val);
  382. regmap_read(wcd9378->regmap,
  383. WCD9378_ANA_MBHC_RESULT_2, &val1);
  384. val = val << 0x08;
  385. val |= val1;
  386. x1 = WCD9378_MBHC_GET_X1(val);
  387. i++;
  388. if (i == WCD9378_ZDET_NUM_MEASUREMENTS)
  389. break;
  390. }
  391. }
  392. static void wcd9378_mbhc_zdet_ramp(struct snd_soc_component *component,
  393. struct wcd9378_mbhc_zdet_param *zdet_param,
  394. int32_t *zl, int32_t *zr, s16 *d1_a)
  395. {
  396. struct wcd9378_priv *wcd9378 = dev_get_drvdata(component->dev);
  397. int32_t zdet = 0;
  398. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_ZDET_ANA_CTL,
  399. 0x70, zdet_param->ldo_ctl << 4);
  400. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_BTN5, 0xFC,
  401. zdet_param->btn5);
  402. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_BTN6, 0xFC,
  403. zdet_param->btn6);
  404. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_BTN7, 0xFC,
  405. zdet_param->btn7);
  406. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_ZDET_ANA_CTL,
  407. 0x0F, zdet_param->noff);
  408. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_ZDET_RAMP_CTL,
  409. 0x0F, zdet_param->nshift);
  410. if (!zl)
  411. goto z_right;
  412. /* Start impedance measurement for HPH_L */
  413. regmap_update_bits(wcd9378->regmap,
  414. WCD9378_ANA_MBHC_ZDET, 0x80, 0x80);
  415. dev_dbg(wcd9378->dev, "%s: ramp for HPH_L, noff = %d\n",
  416. __func__, zdet_param->noff);
  417. wcd9378_mbhc_get_result_params(wcd9378, d1_a, zdet_param->noff, &zdet);
  418. regmap_update_bits(wcd9378->regmap,
  419. WCD9378_ANA_MBHC_ZDET, 0x80, 0x00);
  420. *zl = zdet;
  421. z_right:
  422. if (!zr)
  423. return;
  424. /* Start impedance measurement for HPH_R */
  425. regmap_update_bits(wcd9378->regmap,
  426. WCD9378_ANA_MBHC_ZDET, 0x40, 0x40);
  427. dev_dbg(wcd9378->dev, "%s: ramp for HPH_R, noff = %d\n",
  428. __func__, zdet_param->noff);
  429. wcd9378_mbhc_get_result_params(wcd9378, d1_a, zdet_param->noff, &zdet);
  430. regmap_update_bits(wcd9378->regmap,
  431. WCD9378_ANA_MBHC_ZDET, 0x40, 0x00);
  432. *zr = zdet;
  433. }
  434. static inline void wcd9378_wcd_mbhc_qfuse_cal(
  435. struct snd_soc_component *component,
  436. int32_t *z_val, int flag_l_r)
  437. {
  438. s16 q1;
  439. int q1_cal;
  440. if (*z_val < (WCD9378_ZDET_VAL_400/1000))
  441. q1 = snd_soc_component_read(component,
  442. WCD9378_EFUSE_REG_23 + (2 * flag_l_r));
  443. else
  444. q1 = snd_soc_component_read(component,
  445. WCD9378_EFUSE_REG_24 + (2 * flag_l_r));
  446. if (q1 & 0x80)
  447. q1_cal = (10000 - ((q1 & 0x7F) * 25));
  448. else
  449. q1_cal = (10000 + (q1 * 25));
  450. if (q1_cal > 0)
  451. *z_val = ((*z_val) * 10000) / q1_cal;
  452. }
  453. static void wcd9378_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
  454. uint32_t *zr)
  455. {
  456. struct snd_soc_component *component = mbhc->component;
  457. struct wcd9378_priv *wcd9378 = dev_get_drvdata(component->dev);
  458. s16 reg0, reg1, reg2, reg3, reg4;
  459. int32_t z1L, z1R, z1Ls;
  460. int zMono, z_diff1, z_diff2;
  461. bool is_fsm_disable = false;
  462. struct wcd9378_mbhc_zdet_param zdet_param[] = {
  463. {4, 0, 4, 0x08, 0x14, 0x18}, /* < 32ohm */
  464. {2, 0, 3, 0x18, 0x7C, 0x90}, /* 32ohm < Z < 400ohm */
  465. {1, 4, 5, 0x18, 0x7C, 0x90}, /* 400ohm < Z < 1200ohm */
  466. {1, 6, 7, 0x18, 0x7C, 0x90}, /* >1200ohm */
  467. };
  468. struct wcd9378_mbhc_zdet_param *zdet_param_ptr = NULL;
  469. s16 d1_a[][4] = {
  470. {0, 30, 90, 30},
  471. {0, 30, 30, 5},
  472. {0, 30, 30, 5},
  473. {0, 30, 30, 5},
  474. };
  475. s16 *d1 = NULL;
  476. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  477. reg0 = snd_soc_component_read(component, WCD9378_ANA_MBHC_BTN5);
  478. reg1 = snd_soc_component_read(component, WCD9378_ANA_MBHC_BTN6);
  479. reg2 = snd_soc_component_read(component, WCD9378_ANA_MBHC_BTN7);
  480. reg3 = snd_soc_component_read(component, WCD9378_MBHC_CTL_CLK);
  481. reg4 = snd_soc_component_read(component, WCD9378_MBHC_NEW_ZDET_ANA_CTL);
  482. if (snd_soc_component_read(component, WCD9378_ANA_MBHC_ELECT) & 0x80) {
  483. is_fsm_disable = true;
  484. regmap_update_bits(wcd9378->regmap,
  485. WCD9378_ANA_MBHC_ELECT, 0x80, 0x00);
  486. }
  487. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  488. if (mbhc->hphl_swh)
  489. regmap_update_bits(wcd9378->regmap,
  490. WCD9378_ANA_MBHC_MECH, 0x80, 0x00);
  491. /* Turn off 100k pull down on HPHL */
  492. regmap_update_bits(wcd9378->regmap,
  493. WCD9378_ANA_MBHC_MECH, 0x01, 0x00);
  494. /* Disable surge protection before impedance detection.
  495. * This is done to give correct value for high impedance.
  496. */
  497. regmap_update_bits(wcd9378->regmap,
  498. WCD9378_HPH_SURGE_HPHLR_SURGE_EN, 0xC0, 0x00);
  499. /* 1ms delay needed after disable surge protection */
  500. usleep_range(1000, 1010);
  501. /* First get impedance on Left */
  502. d1 = d1_a[1];
  503. zdet_param_ptr = &zdet_param[1];
  504. wcd9378_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  505. if (!WCD9378_MBHC_IS_SECOND_RAMP_REQUIRED(z1L))
  506. goto left_ch_impedance;
  507. /* Second ramp for left ch */
  508. if (z1L < WCD9378_ZDET_VAL_32) {
  509. zdet_param_ptr = &zdet_param[0];
  510. d1 = d1_a[0];
  511. } else if ((z1L > WCD9378_ZDET_VAL_400) &&
  512. (z1L <= WCD9378_ZDET_VAL_1200)) {
  513. zdet_param_ptr = &zdet_param[2];
  514. d1 = d1_a[2];
  515. } else if (z1L > WCD9378_ZDET_VAL_1200) {
  516. zdet_param_ptr = &zdet_param[3];
  517. d1 = d1_a[3];
  518. }
  519. wcd9378_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  520. left_ch_impedance:
  521. if ((z1L == WCD9378_ZDET_FLOATING_IMPEDANCE) ||
  522. (z1L > WCD9378_ZDET_VAL_100K)) {
  523. *zl = WCD9378_ZDET_FLOATING_IMPEDANCE;
  524. zdet_param_ptr = &zdet_param[1];
  525. d1 = d1_a[1];
  526. } else {
  527. *zl = z1L/1000;
  528. wcd9378_wcd_mbhc_qfuse_cal(component, zl, 0);
  529. }
  530. dev_dbg(component->dev, "%s: impedance on HPH_L = %d(ohms)\n",
  531. __func__, *zl);
  532. /* Start of right impedance ramp and calculation */
  533. wcd9378_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
  534. if (WCD9378_MBHC_IS_SECOND_RAMP_REQUIRED(z1R)) {
  535. if (((z1R > WCD9378_ZDET_VAL_1200) &&
  536. (zdet_param_ptr->noff == 0x6)) ||
  537. ((*zl) != WCD9378_ZDET_FLOATING_IMPEDANCE))
  538. goto right_ch_impedance;
  539. /* Second ramp for right ch */
  540. if (z1R < WCD9378_ZDET_VAL_32) {
  541. zdet_param_ptr = &zdet_param[0];
  542. d1 = d1_a[0];
  543. } else if ((z1R > WCD9378_ZDET_VAL_400) &&
  544. (z1R <= WCD9378_ZDET_VAL_1200)) {
  545. zdet_param_ptr = &zdet_param[2];
  546. d1 = d1_a[2];
  547. } else if (z1R > WCD9378_ZDET_VAL_1200) {
  548. zdet_param_ptr = &zdet_param[3];
  549. d1 = d1_a[3];
  550. }
  551. wcd9378_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
  552. }
  553. right_ch_impedance:
  554. if ((z1R == WCD9378_ZDET_FLOATING_IMPEDANCE) ||
  555. (z1R > WCD9378_ZDET_VAL_100K)) {
  556. *zr = WCD9378_ZDET_FLOATING_IMPEDANCE;
  557. } else {
  558. *zr = z1R/1000;
  559. wcd9378_wcd_mbhc_qfuse_cal(component, zr, 1);
  560. }
  561. dev_dbg(component->dev, "%s: impedance on HPH_R = %d(ohms)\n",
  562. __func__, *zr);
  563. /* Mono/stereo detection */
  564. if ((*zl == WCD9378_ZDET_FLOATING_IMPEDANCE) &&
  565. (*zr == WCD9378_ZDET_FLOATING_IMPEDANCE)) {
  566. dev_dbg(component->dev,
  567. "%s: plug type is invalid or extension cable\n",
  568. __func__);
  569. goto zdet_complete;
  570. }
  571. if ((*zl == WCD9378_ZDET_FLOATING_IMPEDANCE) ||
  572. (*zr == WCD9378_ZDET_FLOATING_IMPEDANCE) ||
  573. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  574. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  575. dev_dbg(component->dev,
  576. "%s: Mono plug type with one ch floating or shorted to GND\n",
  577. __func__);
  578. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  579. goto zdet_complete;
  580. }
  581. snd_soc_component_update_bits(component, WCD9378_HPH_R_ATEST, 0x01, 0x01);
  582. snd_soc_component_update_bits(component, WCD9378_HPH_PA_CTL2, 0x40, 0x01);
  583. if (*zl < (WCD9378_ZDET_VAL_32/1000))
  584. wcd9378_mbhc_zdet_ramp(component, &zdet_param[0], &z1Ls, NULL, d1);
  585. else
  586. wcd9378_mbhc_zdet_ramp(component, &zdet_param[1], &z1Ls, NULL, d1);
  587. snd_soc_component_update_bits(component, WCD9378_HPH_PA_CTL2, 0x40, 0x00);
  588. snd_soc_component_update_bits(component, WCD9378_HPH_R_ATEST, 0x01, 0x00);
  589. z1Ls /= 1000;
  590. wcd9378_wcd_mbhc_qfuse_cal(component, &z1Ls, 0);
  591. /* Parallel of left Z and 9 ohm pull down resistor */
  592. zMono = ((*zl) * 9) / ((*zl) + 9);
  593. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  594. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  595. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  596. dev_dbg(component->dev, "%s: stereo plug type detected\n",
  597. __func__);
  598. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  599. } else {
  600. dev_dbg(component->dev, "%s: MONO plug type detected\n",
  601. __func__);
  602. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  603. }
  604. /* Enable surge protection again after impedance detection */
  605. regmap_update_bits(wcd9378->regmap,
  606. WCD9378_HPH_SURGE_HPHLR_SURGE_EN, 0xC0, 0xC0);
  607. zdet_complete:
  608. snd_soc_component_write(component, WCD9378_ANA_MBHC_BTN5, reg0);
  609. snd_soc_component_write(component, WCD9378_ANA_MBHC_BTN6, reg1);
  610. snd_soc_component_write(component, WCD9378_ANA_MBHC_BTN7, reg2);
  611. /* Turn on 100k pull down on HPHL */
  612. regmap_update_bits(wcd9378->regmap,
  613. WCD9378_ANA_MBHC_MECH, 0x01, 0x01);
  614. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  615. if (mbhc->hphl_swh)
  616. regmap_update_bits(wcd9378->regmap,
  617. WCD9378_ANA_MBHC_MECH, 0x80, 0x80);
  618. snd_soc_component_write(component, WCD9378_MBHC_NEW_ZDET_ANA_CTL, reg4);
  619. snd_soc_component_write(component, WCD9378_MBHC_CTL_CLK, reg3);
  620. if (is_fsm_disable)
  621. regmap_update_bits(wcd9378->regmap,
  622. WCD9378_ANA_MBHC_ELECT, 0x80, 0x80);
  623. }
  624. static void wcd9378_mbhc_gnd_det_ctrl(struct snd_soc_component *component,
  625. bool enable)
  626. {
  627. if (enable) {
  628. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_MECH,
  629. 0x02, 0x02);
  630. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_MECH,
  631. 0x40, 0x40);
  632. } else {
  633. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_MECH,
  634. 0x40, 0x00);
  635. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_MECH,
  636. 0x02, 0x00);
  637. }
  638. }
  639. static void wcd9378_mbhc_hph_pull_down_ctrl(struct snd_soc_component *component,
  640. bool enable)
  641. {
  642. if (enable) {
  643. snd_soc_component_update_bits(component, WCD9378_HPH_PA_CTL2,
  644. 0x40, 0x40);
  645. snd_soc_component_update_bits(component, WCD9378_HPH_PA_CTL2,
  646. 0x10, 0x10);
  647. } else {
  648. snd_soc_component_update_bits(component, WCD9378_HPH_PA_CTL2,
  649. 0x40, 0x00);
  650. snd_soc_component_update_bits(component, WCD9378_HPH_PA_CTL2,
  651. 0x10, 0x00);
  652. }
  653. }
  654. static void wcd9378_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  655. {
  656. struct snd_soc_component *component = mbhc->component;
  657. if ((mbhc->moist_rref == R_OFF) ||
  658. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  659. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_2,
  660. 0x0C, R_OFF << 2);
  661. return;
  662. }
  663. /* Do not enable moisture detection if jack type is NC */
  664. if (!mbhc->hphl_swh) {
  665. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  666. __func__);
  667. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_2,
  668. 0x0C, R_OFF << 2);
  669. return;
  670. }
  671. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_2,
  672. 0x0C, mbhc->moist_rref << 2);
  673. }
  674. static void wcd9378_mbhc_moisture_detect_en(struct wcd_mbhc *mbhc, bool enable)
  675. {
  676. struct snd_soc_component *component = mbhc->component;
  677. if (enable)
  678. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_2,
  679. 0x0C, mbhc->moist_rref << 2);
  680. else
  681. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_2,
  682. 0x0C, R_OFF << 2);
  683. }
  684. static bool wcd9378_mbhc_get_moisture_status(struct wcd_mbhc *mbhc)
  685. {
  686. struct snd_soc_component *component = mbhc->component;
  687. bool ret = false;
  688. if ((mbhc->moist_rref == R_OFF) ||
  689. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  690. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_2,
  691. 0x0C, R_OFF << 2);
  692. goto done;
  693. }
  694. /* Do not enable moisture detection if jack type is NC */
  695. if (!mbhc->hphl_swh) {
  696. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  697. __func__);
  698. snd_soc_component_update_bits(component, WCD9378_MBHC_NEW_CTL_2,
  699. 0x0C, R_OFF << 2);
  700. goto done;
  701. }
  702. /*
  703. * If moisture_en is already enabled, then skip to plug type
  704. * detection.
  705. */
  706. if ((snd_soc_component_read(component, WCD9378_MBHC_NEW_CTL_2) & 0x0C))
  707. goto done;
  708. wcd9378_mbhc_moisture_detect_en(mbhc, true);
  709. /* Read moisture comparator status */
  710. ret = ((snd_soc_component_read(component, WCD9378_MBHC_NEW_FSM_STATUS)
  711. & 0x20) ? 0 : 1);
  712. done:
  713. return ret;
  714. }
  715. static void wcd9378_mbhc_moisture_polling_ctrl(struct wcd_mbhc *mbhc,
  716. bool enable)
  717. {
  718. struct snd_soc_component *component = mbhc->component;
  719. snd_soc_component_update_bits(component,
  720. WCD9378_MBHC_NEW_INT_MOISTURE_DET_POLLING_CTRL,
  721. 0x04, (enable << 2));
  722. }
  723. static void wcd9378_mbhc_bcs_enable(struct wcd_mbhc *mbhc,
  724. bool bcs_enable)
  725. {
  726. if (bcs_enable)
  727. wcd9378_disable_bcs_before_slow_insert(mbhc->component, false);
  728. else
  729. wcd9378_disable_bcs_before_slow_insert(mbhc->component, true);
  730. }
  731. static const struct wcd_mbhc_cb mbhc_cb = {
  732. .request_irq = wcd9378_mbhc_request_irq,
  733. .irq_control = wcd9378_mbhc_irq_control,
  734. .free_irq = wcd9378_mbhc_free_irq,
  735. .clk_setup = wcd9378_mbhc_clk_setup,
  736. .map_btn_code_to_num = wcd9378_mbhc_btn_to_num,
  737. .mbhc_bias = wcd9378_mbhc_mbhc_bias_control,
  738. .set_btn_thr = wcd9378_mbhc_program_btn_thr,
  739. .lock_sleep = wcd9378_mbhc_lock_sleep,
  740. .register_notifier = wcd9378_mbhc_register_notifier,
  741. .micbias_enable_status = wcd9378_mbhc_micb_en_status,
  742. .hph_pa_on_status = wcd9378_mbhc_hph_pa_on_status,
  743. .hph_pull_up_control_v2 = wcd9378_mbhc_hph_l_pull_up_control,
  744. .mbhc_micbias_control = wcd9378_mbhc_request_micbias,
  745. .mbhc_micb_ramp_control = wcd9378_mbhc_micb_ramp_control,
  746. .get_hwdep_fw_cal = wcd9378_get_hwdep_fw_cal,
  747. .mbhc_micb_ctrl_thr_mic = wcd9378_mbhc_micb_ctrl_threshold_mic,
  748. .compute_impedance = wcd9378_wcd_mbhc_calc_impedance,
  749. .mbhc_gnd_det_ctrl = wcd9378_mbhc_gnd_det_ctrl,
  750. .hph_pull_down_ctrl = wcd9378_mbhc_hph_pull_down_ctrl,
  751. .mbhc_moisture_config = wcd9378_mbhc_moisture_config,
  752. .mbhc_get_moisture_status = wcd9378_mbhc_get_moisture_status,
  753. .mbhc_moisture_polling_ctrl = wcd9378_mbhc_moisture_polling_ctrl,
  754. .mbhc_moisture_detect_en = wcd9378_mbhc_moisture_detect_en,
  755. .bcs_enable = wcd9378_mbhc_bcs_enable,
  756. };
  757. static int wcd9378_get_hph_type(struct snd_kcontrol *kcontrol,
  758. struct snd_ctl_elem_value *ucontrol)
  759. {
  760. struct snd_soc_component *component =
  761. snd_soc_kcontrol_component(kcontrol);
  762. struct wcd9378_mbhc *wcd9378_mbhc = wcd9378_soc_get_mbhc(component);
  763. struct wcd_mbhc *mbhc;
  764. if (!wcd9378_mbhc) {
  765. dev_err(component->dev, "%s: mbhc not initialized!\n", __func__);
  766. return -EINVAL;
  767. }
  768. mbhc = &wcd9378_mbhc->wcd_mbhc;
  769. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  770. dev_dbg(component->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  771. return 0;
  772. }
  773. static int wcd9378_hph_impedance_get(struct snd_kcontrol *kcontrol,
  774. struct snd_ctl_elem_value *ucontrol)
  775. {
  776. uint32_t zl, zr;
  777. bool hphr;
  778. struct soc_multi_mixer_control *mc;
  779. struct snd_soc_component *component =
  780. snd_soc_kcontrol_component(kcontrol);
  781. struct wcd9378_mbhc *wcd9378_mbhc = wcd9378_soc_get_mbhc(component);
  782. if (!wcd9378_mbhc) {
  783. dev_err(component->dev, "%s: mbhc not initialized!\n", __func__);
  784. return -EINVAL;
  785. }
  786. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  787. hphr = mc->shift;
  788. wcd_mbhc_get_impedance(&wcd9378_mbhc->wcd_mbhc, &zl, &zr);
  789. dev_dbg(component->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  790. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  791. return 0;
  792. }
  793. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  794. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  795. wcd9378_get_hph_type, NULL),
  796. };
  797. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  798. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  799. wcd9378_hph_impedance_get, NULL),
  800. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  801. wcd9378_hph_impedance_get, NULL),
  802. };
  803. /*
  804. * wcd9378_mbhc_get_impedance: get impedance of headphone
  805. * left and right channels
  806. * @wcd9378_mbhc: handle to struct wcd9378_mbhc *
  807. * @zl: handle to left-ch impedance
  808. * @zr: handle to right-ch impedance
  809. * return 0 for success or error code in case of failure
  810. */
  811. int wcd9378_mbhc_get_impedance(struct wcd9378_mbhc *wcd9378_mbhc,
  812. uint32_t *zl, uint32_t *zr)
  813. {
  814. if (!wcd9378_mbhc) {
  815. pr_err("%s: mbhc not initialized!\n", __func__);
  816. return -EINVAL;
  817. }
  818. if (!zl || !zr) {
  819. pr_err("%s: zl or zr null!\n", __func__);
  820. return -EINVAL;
  821. }
  822. return wcd_mbhc_get_impedance(&wcd9378_mbhc->wcd_mbhc, zl, zr);
  823. }
  824. EXPORT_SYMBOL_GPL(wcd9378_mbhc_get_impedance);
  825. /*
  826. * wcd9378_mbhc_hs_detect: starts mbhc insertion/removal functionality
  827. * @codec: handle to snd_soc_component *
  828. * @mbhc_cfg: handle to mbhc configuration structure
  829. * return 0 if mbhc_start is success or error code in case of failure
  830. */
  831. int wcd9378_mbhc_hs_detect(struct snd_soc_component *component,
  832. struct wcd_mbhc_config *mbhc_cfg)
  833. {
  834. struct wcd9378_priv *wcd9378 = NULL;
  835. struct wcd9378_mbhc *wcd9378_mbhc = NULL;
  836. if (!component) {
  837. pr_err("%s: component is NULL\n", __func__);
  838. return -EINVAL;
  839. }
  840. wcd9378 = snd_soc_component_get_drvdata(component);
  841. if (!wcd9378) {
  842. pr_err("%s: wcd9378 is NULL\n", __func__);
  843. return -EINVAL;
  844. }
  845. wcd9378_mbhc = wcd9378->mbhc;
  846. if (!wcd9378_mbhc) {
  847. dev_err(component->dev, "%s: mbhc not initialized!\n", __func__);
  848. return -EINVAL;
  849. }
  850. return wcd_mbhc_start(&wcd9378_mbhc->wcd_mbhc, mbhc_cfg);
  851. }
  852. EXPORT_SYMBOL_GPL(wcd9378_mbhc_hs_detect);
  853. /*
  854. * wcd9378_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  855. * @component: handle to snd_soc_component *
  856. */
  857. void wcd9378_mbhc_hs_detect_exit(struct snd_soc_component *component)
  858. {
  859. struct wcd9378_priv *wcd9378 = NULL;
  860. struct wcd9378_mbhc *wcd9378_mbhc = NULL;
  861. if (!component) {
  862. pr_err("%s: component is NULL\n", __func__);
  863. return;
  864. }
  865. wcd9378 = snd_soc_component_get_drvdata(component);
  866. if (!wcd9378) {
  867. pr_err("%s: wcd9378 is NULL\n", __func__);
  868. return;
  869. }
  870. wcd9378_mbhc = wcd9378->mbhc;
  871. if (!wcd9378_mbhc) {
  872. dev_err(component->dev, "%s: mbhc not initialized!\n", __func__);
  873. return;
  874. }
  875. wcd_mbhc_stop(&wcd9378_mbhc->wcd_mbhc);
  876. }
  877. EXPORT_SYMBOL_GPL(wcd9378_mbhc_hs_detect_exit);
  878. /*
  879. * wcd9378_mbhc_ssr_down: stop mbhc during
  880. * wcd9378 subsystem restart
  881. * mbhc: pointer to wcd937x_mbhc structure
  882. * component: handle to snd_soc_component *
  883. */
  884. void wcd9378_mbhc_ssr_down(struct wcd9378_mbhc *mbhc,
  885. struct snd_soc_component *component)
  886. {
  887. struct wcd_mbhc *wcd_mbhc = NULL;
  888. if (!mbhc || !component)
  889. return;
  890. wcd_mbhc = &mbhc->wcd_mbhc;
  891. if (!wcd_mbhc) {
  892. dev_err(component->dev, "%s: wcd_mbhc is NULL\n", __func__);
  893. return;
  894. }
  895. wcd9378_mbhc_hs_detect_exit(component);
  896. wcd_mbhc_deinit(wcd_mbhc);
  897. }
  898. EXPORT_SYMBOL_GPL(wcd9378_mbhc_ssr_down);
  899. /*
  900. * wcd9378_mbhc_post_ssr_init: initialize mbhc for
  901. * wcd9378 post subsystem restart
  902. * @mbhc: poniter to wcd9378_mbhc structure
  903. * @component: handle to snd_soc_component *
  904. *
  905. * return 0 if mbhc_init is success or error code in case of failure
  906. */
  907. int wcd9378_mbhc_post_ssr_init(struct wcd9378_mbhc *mbhc,
  908. struct snd_soc_component *component)
  909. {
  910. int ret = 0;
  911. struct wcd_mbhc *wcd_mbhc = NULL;
  912. if (!mbhc || !component)
  913. return -EINVAL;
  914. wcd_mbhc = &mbhc->wcd_mbhc;
  915. if (wcd_mbhc == NULL) {
  916. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  917. return -EINVAL;
  918. }
  919. /* Reset detection type to insertion after SSR recovery */
  920. snd_soc_component_update_bits(component, WCD9378_ANA_MBHC_MECH,
  921. 0x20, 0x20);
  922. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb, &intr_ids,
  923. wcd_mbhc_registers, WCD9378_ZDET_SUPPORTED);
  924. if (ret) {
  925. dev_err(component->dev, "%s: mbhc initialization failed\n",
  926. __func__);
  927. goto done;
  928. }
  929. done:
  930. return ret;
  931. }
  932. EXPORT_SYMBOL_GPL(wcd9378_mbhc_post_ssr_init);
  933. /*
  934. * wcd9378_mbhc_init: initialize mbhc for wcd9378
  935. * @mbhc: poniter to wcd9378_mbhc struct pointer to store the configs
  936. * @codec: handle to snd_soc_component *
  937. * @fw_data: handle to firmware data
  938. *
  939. * return 0 if mbhc_init is success or error code in case of failure
  940. */
  941. int wcd9378_mbhc_init(struct wcd9378_mbhc **mbhc,
  942. struct snd_soc_component *component)
  943. {
  944. struct wcd9378_mbhc *wcd9378_mbhc = NULL;
  945. struct wcd_mbhc *wcd_mbhc = NULL;
  946. int ret = 0;
  947. struct wcd9378_pdata *pdata;
  948. if (!component) {
  949. pr_err("%s: component is NULL\n", __func__);
  950. return -EINVAL;
  951. }
  952. wcd9378_mbhc = devm_kzalloc(component->dev, sizeof(struct wcd9378_mbhc),
  953. GFP_KERNEL);
  954. if (!wcd9378_mbhc)
  955. return -ENOMEM;
  956. BLOCKING_INIT_NOTIFIER_HEAD(&wcd9378_mbhc->notifier);
  957. wcd_mbhc = &wcd9378_mbhc->wcd_mbhc;
  958. if (wcd_mbhc == NULL) {
  959. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  960. ret = -EINVAL;
  961. goto err;
  962. }
  963. /* Setting default mbhc detection logic to ADC */
  964. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  965. pdata = dev_get_platdata(component->dev);
  966. if (!pdata) {
  967. dev_err(component->dev, "%s: pdata pointer is NULL\n",
  968. __func__);
  969. ret = -EINVAL;
  970. goto err;
  971. }
  972. wcd_mbhc->micb_mv = pdata->micbias.micb2_mv;
  973. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb,
  974. &intr_ids, wcd_mbhc_registers,
  975. WCD9378_ZDET_SUPPORTED);
  976. if (ret) {
  977. dev_err(component->dev, "%s: mbhc initialization failed\n",
  978. __func__);
  979. goto err;
  980. }
  981. (*mbhc) = wcd9378_mbhc;
  982. snd_soc_add_component_controls(component, impedance_detect_controls,
  983. ARRAY_SIZE(impedance_detect_controls));
  984. snd_soc_add_component_controls(component, hph_type_detect_controls,
  985. ARRAY_SIZE(hph_type_detect_controls));
  986. return 0;
  987. err:
  988. return ret;
  989. }
  990. EXPORT_SYMBOL_GPL(wcd9378_mbhc_init);
  991. /*
  992. * wcd9378_mbhc_deinit: deinitialize mbhc for wcd9378
  993. * @codec: handle to snd_soc_component *
  994. */
  995. void wcd9378_mbhc_deinit(struct snd_soc_component *component)
  996. {
  997. struct wcd9378_priv *wcd9378;
  998. struct wcd9378_mbhc *wcd9378_mbhc;
  999. if (!component) {
  1000. pr_err("%s: component is NULL\n", __func__);
  1001. return;
  1002. }
  1003. wcd9378 = snd_soc_component_get_drvdata(component);
  1004. if (!wcd9378) {
  1005. pr_err("%s: wcd9378 is NULL\n", __func__);
  1006. return;
  1007. }
  1008. wcd9378_mbhc = wcd9378->mbhc;
  1009. if (wcd9378_mbhc)
  1010. wcd_mbhc_deinit(&wcd9378_mbhc->wcd_mbhc);
  1011. }
  1012. EXPORT_SYMBOL_GPL(wcd9378_mbhc_deinit);