wcd937x-mbhc.c 32 KB

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