wcd937x-mbhc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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_bulk_read(wcd937x->regmap,
  372. WCD937X_ANA_MBHC_RESULT_1, (u8 *)&val, 2);
  373. x1 = WCD937X_MBHC_GET_X1(val);
  374. i++;
  375. if (i == WCD937X_ZDET_NUM_MEASUREMENTS)
  376. break;
  377. }
  378. }
  379. static void wcd937x_mbhc_zdet_ramp(struct snd_soc_codec *codec,
  380. struct wcd937x_mbhc_zdet_param *zdet_param,
  381. int32_t *zl, int32_t *zr, s16 *d1_a)
  382. {
  383. struct wcd937x_priv *wcd937x = dev_get_drvdata(codec->dev);
  384. int32_t zdet = 0;
  385. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_ZDET_ANA_CTL, 0x70,
  386. zdet_param->ldo_ctl << 4);
  387. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_BTN5, 0xFC,
  388. zdet_param->btn5);
  389. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_BTN6, 0xFC,
  390. zdet_param->btn6);
  391. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_BTN7, 0xFC,
  392. zdet_param->btn7);
  393. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_ZDET_ANA_CTL, 0x0F,
  394. zdet_param->noff);
  395. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_ZDET_RAMP_CTL, 0x0F,
  396. zdet_param->nshift);
  397. if (!zl)
  398. goto z_right;
  399. /* Start impedance measurement for HPH_L */
  400. regmap_update_bits(wcd937x->regmap,
  401. WCD937X_ANA_MBHC_ZDET, 0x80, 0x80);
  402. dev_dbg(wcd937x->dev, "%s: ramp for HPH_L, noff = %d\n",
  403. __func__, zdet_param->noff);
  404. wcd937x_mbhc_get_result_params(wcd937x, d1_a, zdet_param->noff, &zdet);
  405. regmap_update_bits(wcd937x->regmap,
  406. WCD937X_ANA_MBHC_ZDET, 0x80, 0x00);
  407. *zl = zdet;
  408. z_right:
  409. if (!zr)
  410. return;
  411. /* Start impedance measurement for HPH_R */
  412. regmap_update_bits(wcd937x->regmap,
  413. WCD937X_ANA_MBHC_ZDET, 0x40, 0x40);
  414. dev_dbg(wcd937x->dev, "%s: ramp for HPH_R, noff = %d\n",
  415. __func__, zdet_param->noff);
  416. wcd937x_mbhc_get_result_params(wcd937x, d1_a, zdet_param->noff, &zdet);
  417. regmap_update_bits(wcd937x->regmap,
  418. WCD937X_ANA_MBHC_ZDET, 0x40, 0x00);
  419. *zr = zdet;
  420. }
  421. static inline void wcd937x_wcd_mbhc_qfuse_cal(struct snd_soc_codec *codec,
  422. int32_t *z_val, int flag_l_r)
  423. {
  424. s16 q1;
  425. int q1_cal;
  426. if (*z_val < (WCD937X_ZDET_VAL_400/1000))
  427. q1 = snd_soc_read(codec,
  428. WCD937X_DIGITAL_EFUSE_REG_23 + (2 * flag_l_r));
  429. else
  430. q1 = snd_soc_read(codec,
  431. WCD937X_DIGITAL_EFUSE_REG_24 + (2 * flag_l_r));
  432. if (q1 & 0x80)
  433. q1_cal = (10000 - ((q1 & 0x7F) * 25));
  434. else
  435. q1_cal = (10000 + (q1 * 25));
  436. if (q1_cal > 0)
  437. *z_val = ((*z_val) * 10000) / q1_cal;
  438. }
  439. static void wcd937x_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
  440. uint32_t *zr)
  441. {
  442. struct snd_soc_codec *codec = mbhc->codec;
  443. struct wcd937x_priv *wcd937x = dev_get_drvdata(codec->dev);
  444. s16 reg0, reg1, reg2, reg3, reg4;
  445. int32_t z1L, z1R, z1Ls;
  446. int zMono, z_diff1, z_diff2;
  447. bool is_fsm_disable = false;
  448. struct wcd937x_mbhc_zdet_param zdet_param[] = {
  449. {4, 0, 4, 0x08, 0x14, 0x18}, /* < 32ohm */
  450. {2, 0, 3, 0x18, 0x7C, 0x90}, /* 32ohm < Z < 400ohm */
  451. {1, 4, 5, 0x18, 0x7C, 0x90}, /* 400ohm < Z < 1200ohm */
  452. {1, 6, 7, 0x18, 0x7C, 0x90}, /* >1200ohm */
  453. };
  454. struct wcd937x_mbhc_zdet_param *zdet_param_ptr = NULL;
  455. s16 d1_a[][4] = {
  456. {0, 30, 90, 30},
  457. {0, 30, 30, 5},
  458. {0, 30, 30, 5},
  459. {0, 30, 30, 5},
  460. };
  461. s16 *d1 = NULL;
  462. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  463. reg0 = snd_soc_read(codec, WCD937X_ANA_MBHC_BTN5);
  464. reg1 = snd_soc_read(codec, WCD937X_ANA_MBHC_BTN6);
  465. reg2 = snd_soc_read(codec, WCD937X_ANA_MBHC_BTN7);
  466. reg3 = snd_soc_read(codec, WCD937X_MBHC_CTL_CLK);
  467. reg4 = snd_soc_read(codec, WCD937X_MBHC_NEW_ZDET_ANA_CTL);
  468. if (snd_soc_read(codec, WCD937X_ANA_MBHC_ELECT) & 0x80) {
  469. is_fsm_disable = true;
  470. regmap_update_bits(wcd937x->regmap,
  471. WCD937X_ANA_MBHC_ELECT, 0x80, 0x00);
  472. }
  473. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  474. if (mbhc->hphl_swh)
  475. regmap_update_bits(wcd937x->regmap,
  476. WCD937X_ANA_MBHC_MECH, 0x80, 0x00);
  477. /* Turn off 100k pull down on HPHL */
  478. regmap_update_bits(wcd937x->regmap,
  479. WCD937X_ANA_MBHC_MECH, 0x01, 0x00);
  480. /* First get impedance on Left */
  481. d1 = d1_a[1];
  482. zdet_param_ptr = &zdet_param[1];
  483. wcd937x_mbhc_zdet_ramp(codec, zdet_param_ptr, &z1L, NULL, d1);
  484. if (!WCD937X_MBHC_IS_SECOND_RAMP_REQUIRED(z1L))
  485. goto left_ch_impedance;
  486. /* Second ramp for left ch */
  487. if (z1L < WCD937X_ZDET_VAL_32) {
  488. zdet_param_ptr = &zdet_param[0];
  489. d1 = d1_a[0];
  490. } else if ((z1L > WCD937X_ZDET_VAL_400) &&
  491. (z1L <= WCD937X_ZDET_VAL_1200)) {
  492. zdet_param_ptr = &zdet_param[2];
  493. d1 = d1_a[2];
  494. } else if (z1L > WCD937X_ZDET_VAL_1200) {
  495. zdet_param_ptr = &zdet_param[3];
  496. d1 = d1_a[3];
  497. }
  498. wcd937x_mbhc_zdet_ramp(codec, zdet_param_ptr, &z1L, NULL, d1);
  499. left_ch_impedance:
  500. if ((z1L == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  501. (z1L > WCD937X_ZDET_VAL_100K)) {
  502. *zl = WCD937X_ZDET_FLOATING_IMPEDANCE;
  503. zdet_param_ptr = &zdet_param[1];
  504. d1 = d1_a[1];
  505. } else {
  506. *zl = z1L/1000;
  507. wcd937x_wcd_mbhc_qfuse_cal(codec, zl, 0);
  508. }
  509. dev_dbg(codec->dev, "%s: impedance on HPH_L = %d(ohms)\n",
  510. __func__, *zl);
  511. /* Start of right impedance ramp and calculation */
  512. wcd937x_mbhc_zdet_ramp(codec, zdet_param_ptr, NULL, &z1R, d1);
  513. if (WCD937X_MBHC_IS_SECOND_RAMP_REQUIRED(z1R)) {
  514. if (((z1R > WCD937X_ZDET_VAL_1200) &&
  515. (zdet_param_ptr->noff == 0x6)) ||
  516. ((*zl) != WCD937X_ZDET_FLOATING_IMPEDANCE))
  517. goto right_ch_impedance;
  518. /* Second ramp for right ch */
  519. if (z1R < WCD937X_ZDET_VAL_32) {
  520. zdet_param_ptr = &zdet_param[0];
  521. d1 = d1_a[0];
  522. } else if ((z1R > WCD937X_ZDET_VAL_400) &&
  523. (z1R <= WCD937X_ZDET_VAL_1200)) {
  524. zdet_param_ptr = &zdet_param[2];
  525. d1 = d1_a[2];
  526. } else if (z1R > WCD937X_ZDET_VAL_1200) {
  527. zdet_param_ptr = &zdet_param[3];
  528. d1 = d1_a[3];
  529. }
  530. wcd937x_mbhc_zdet_ramp(codec, zdet_param_ptr, NULL, &z1R, d1);
  531. }
  532. right_ch_impedance:
  533. if ((z1R == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  534. (z1R > WCD937X_ZDET_VAL_100K)) {
  535. *zr = WCD937X_ZDET_FLOATING_IMPEDANCE;
  536. } else {
  537. *zr = z1R/1000;
  538. wcd937x_wcd_mbhc_qfuse_cal(codec, zr, 1);
  539. }
  540. dev_dbg(codec->dev, "%s: impedance on HPH_R = %d(ohms)\n",
  541. __func__, *zr);
  542. /* Mono/stereo detection */
  543. if ((*zl == WCD937X_ZDET_FLOATING_IMPEDANCE) &&
  544. (*zr == WCD937X_ZDET_FLOATING_IMPEDANCE)) {
  545. dev_dbg(codec->dev,
  546. "%s: plug type is invalid or extension cable\n",
  547. __func__);
  548. goto zdet_complete;
  549. }
  550. if ((*zl == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  551. (*zr == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  552. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  553. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  554. dev_dbg(codec->dev,
  555. "%s: Mono plug type with one ch floating or shorted to GND\n",
  556. __func__);
  557. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  558. goto zdet_complete;
  559. }
  560. snd_soc_update_bits(codec, WCD937X_HPH_R_ATEST, 0x02, 0x02);
  561. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2, 0x40, 0x01);
  562. if (*zl < (WCD937X_ZDET_VAL_32/1000))
  563. wcd937x_mbhc_zdet_ramp(codec, &zdet_param[0], &z1Ls, NULL, d1);
  564. else
  565. wcd937x_mbhc_zdet_ramp(codec, &zdet_param[1], &z1Ls, NULL, d1);
  566. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2, 0x40, 0x00);
  567. snd_soc_update_bits(codec, WCD937X_HPH_R_ATEST, 0x02, 0x00);
  568. z1Ls /= 1000;
  569. wcd937x_wcd_mbhc_qfuse_cal(codec, &z1Ls, 0);
  570. /* Parallel of left Z and 9 ohm pull down resistor */
  571. zMono = ((*zl) * 9) / ((*zl) + 9);
  572. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  573. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  574. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  575. dev_dbg(codec->dev, "%s: stereo plug type detected\n",
  576. __func__);
  577. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  578. } else {
  579. dev_dbg(codec->dev, "%s: MONO plug type detected\n",
  580. __func__);
  581. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  582. }
  583. zdet_complete:
  584. snd_soc_write(codec, WCD937X_ANA_MBHC_BTN5, reg0);
  585. snd_soc_write(codec, WCD937X_ANA_MBHC_BTN6, reg1);
  586. snd_soc_write(codec, WCD937X_ANA_MBHC_BTN7, reg2);
  587. /* Turn on 100k pull down on HPHL */
  588. regmap_update_bits(wcd937x->regmap,
  589. WCD937X_ANA_MBHC_MECH, 0x01, 0x01);
  590. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  591. if (mbhc->hphl_swh)
  592. regmap_update_bits(wcd937x->regmap,
  593. WCD937X_ANA_MBHC_MECH, 0x80, 0x80);
  594. snd_soc_write(codec, WCD937X_MBHC_NEW_ZDET_ANA_CTL, reg4);
  595. snd_soc_write(codec, WCD937X_MBHC_CTL_CLK, reg3);
  596. if (is_fsm_disable)
  597. regmap_update_bits(wcd937x->regmap,
  598. WCD937X_ANA_MBHC_ELECT, 0x80, 0x80);
  599. }
  600. static void wcd937x_mbhc_gnd_det_ctrl(struct snd_soc_codec *codec, bool enable)
  601. {
  602. if (enable) {
  603. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_MECH,
  604. 0x02, 0x02);
  605. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_MECH,
  606. 0x40, 0x40);
  607. } else {
  608. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_MECH,
  609. 0x40, 0x00);
  610. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_MECH,
  611. 0x02, 0x00);
  612. }
  613. }
  614. static void wcd937x_mbhc_hph_pull_down_ctrl(struct snd_soc_codec *codec,
  615. bool enable)
  616. {
  617. if (enable) {
  618. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2,
  619. 0x40, 0x40);
  620. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2,
  621. 0x10, 0x10);
  622. } else {
  623. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2,
  624. 0x40, 0x00);
  625. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2,
  626. 0x10, 0x00);
  627. }
  628. }
  629. static void wcd937x_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  630. {
  631. struct snd_soc_codec *codec = mbhc->codec;
  632. if ((mbhc->moist_rref == R_OFF) ||
  633. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  634. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  635. 0x0C, R_OFF << 2);
  636. return;
  637. }
  638. /* Do not enable moisture detection if jack type is NC */
  639. if (!mbhc->hphl_swh) {
  640. dev_dbg(codec->dev, "%s: disable moisture detection for NC\n",
  641. __func__);
  642. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  643. 0x0C, R_OFF << 2);
  644. return;
  645. }
  646. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  647. 0x0C, mbhc->moist_rref << 2);
  648. }
  649. static void wcd937x_mbhc_moisture_detect_en(struct wcd_mbhc *mbhc, bool enable)
  650. {
  651. struct snd_soc_codec *codec = mbhc->codec;
  652. if (enable)
  653. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  654. 0x0C, mbhc->moist_rref << 2);
  655. else
  656. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  657. 0x0C, R_OFF << 2);
  658. }
  659. static bool wcd937x_mbhc_get_moisture_status(struct wcd_mbhc *mbhc)
  660. {
  661. struct snd_soc_codec *codec = mbhc->codec;
  662. bool ret = false;
  663. if ((mbhc->moist_rref == R_OFF) ||
  664. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  665. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  666. 0x0C, R_OFF << 2);
  667. goto done;
  668. }
  669. /* Do not enable moisture detection if jack type is NC */
  670. if (!mbhc->hphl_swh) {
  671. dev_dbg(codec->dev, "%s: disable moisture detection for NC\n",
  672. __func__);
  673. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  674. 0x0C, R_OFF << 2);
  675. goto done;
  676. }
  677. /* If moisture_en is already enabled, then skip to plug type
  678. * detection.
  679. */
  680. if ((snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2) & 0x0C))
  681. goto done;
  682. wcd937x_mbhc_moisture_detect_en(mbhc, true);
  683. /* Read moisture comparator status */
  684. ret = ((snd_soc_read(codec, WCD937X_MBHC_NEW_FSM_STATUS)
  685. & 0x20) ? 0 : 1);
  686. done:
  687. return ret;
  688. }
  689. static void wcd937x_mbhc_moisture_polling_ctrl(struct wcd_mbhc *mbhc,
  690. bool enable)
  691. {
  692. snd_soc_update_bits(codec,
  693. WCD937X_MBHC_NEW_INT_MOISTURE_DET_POLLING_CTRL,
  694. 0x04, (enable << 2));
  695. }
  696. static const struct wcd_mbhc_cb mbhc_cb = {
  697. .request_irq = wcd937x_mbhc_request_irq,
  698. .irq_control = wcd937x_mbhc_irq_control,
  699. .free_irq = wcd937x_mbhc_free_irq,
  700. .clk_setup = wcd937x_mbhc_clk_setup,
  701. .map_btn_code_to_num = wcd937x_mbhc_btn_to_num,
  702. .mbhc_bias = wcd937x_mbhc_mbhc_bias_control,
  703. .set_btn_thr = wcd937x_mbhc_program_btn_thr,
  704. .lock_sleep = wcd937x_mbhc_lock_sleep,
  705. .register_notifier = wcd937x_mbhc_register_notifier,
  706. .micbias_enable_status = wcd937x_mbhc_micb_en_status,
  707. .hph_pa_on_status = wcd937x_mbhc_hph_pa_on_status,
  708. .hph_pull_up_control_v2 = wcd937x_mbhc_hph_l_pull_up_control,
  709. .mbhc_micbias_control = wcd937x_mbhc_request_micbias,
  710. .mbhc_micb_ramp_control = wcd937x_mbhc_micb_ramp_control,
  711. .get_hwdep_fw_cal = wcd937x_get_hwdep_fw_cal,
  712. .mbhc_micb_ctrl_thr_mic = wcd937x_mbhc_micb_ctrl_threshold_mic,
  713. .compute_impedance = wcd937x_wcd_mbhc_calc_impedance,
  714. .mbhc_gnd_det_ctrl = wcd937x_mbhc_gnd_det_ctrl,
  715. .hph_pull_down_ctrl = wcd937x_mbhc_hph_pull_down_ctrl,
  716. .mbhc_moisture_config = wcd937x_mbhc_moisture_config,
  717. .mbhc_get_moisture_status = wcd937x_mbhc_get_moisture_status,
  718. .mbhc_moisture_polling_ctrl = wcd937x_mbhc_moisture_polling_ctrl,
  719. .mbhc_moisture_detect_en = wcd937x_mbhc_moisture_detect_en,
  720. };
  721. static int wcd937x_get_hph_type(struct snd_kcontrol *kcontrol,
  722. struct snd_ctl_elem_value *ucontrol)
  723. {
  724. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  725. struct wcd937x_mbhc *wcd937x_mbhc = wcd937x_soc_get_mbhc(codec);
  726. struct wcd_mbhc *mbhc;
  727. if (!wcd937x_mbhc) {
  728. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  729. return -EINVAL;
  730. }
  731. mbhc = &wcd937x_mbhc->wcd_mbhc;
  732. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  733. dev_dbg(codec->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  734. return 0;
  735. }
  736. static int wcd937x_hph_impedance_get(struct snd_kcontrol *kcontrol,
  737. struct snd_ctl_elem_value *ucontrol)
  738. {
  739. uint32_t zl, zr;
  740. bool hphr;
  741. struct soc_multi_mixer_control *mc;
  742. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  743. struct wcd937x_mbhc *wcd937x_mbhc = wcd937x_soc_get_mbhc(codec);
  744. if (!wcd937x_mbhc) {
  745. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  746. return -EINVAL;
  747. }
  748. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  749. hphr = mc->shift;
  750. wcd_mbhc_get_impedance(&wcd937x_mbhc->wcd_mbhc, &zl, &zr);
  751. dev_dbg(codec->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  752. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  753. return 0;
  754. }
  755. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  756. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  757. wcd937x_get_hph_type, NULL),
  758. };
  759. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  760. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  761. wcd937x_hph_impedance_get, NULL),
  762. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  763. wcd937x_hph_impedance_get, NULL),
  764. };
  765. /*
  766. * wcd937x_mbhc_get_impedance: get impedance of headphone
  767. * left and right channels
  768. * @wcd937x_mbhc: handle to struct wcd937x_mbhc *
  769. * @zl: handle to left-ch impedance
  770. * @zr: handle to right-ch impedance
  771. * return 0 for success or error code in case of failure
  772. */
  773. int wcd937x_mbhc_get_impedance(struct wcd937x_mbhc *wcd937x_mbhc,
  774. uint32_t *zl, uint32_t *zr)
  775. {
  776. if (!wcd937x_mbhc) {
  777. pr_err("%s: mbhc not initialized!\n", __func__);
  778. return -EINVAL;
  779. }
  780. if (!zl || !zr) {
  781. pr_err("%s: zl or zr null!\n", __func__);
  782. return -EINVAL;
  783. }
  784. return wcd_mbhc_get_impedance(&wcd937x_mbhc->wcd_mbhc, zl, zr);
  785. }
  786. EXPORT_SYMBOL(wcd937x_mbhc_get_impedance);
  787. /*
  788. * wcd937x_mbhc_hs_detect: starts mbhc insertion/removal functionality
  789. * @codec: handle to snd_soc_codec *
  790. * @mbhc_cfg: handle to mbhc configuration structure
  791. * return 0 if mbhc_start is success or error code in case of failure
  792. */
  793. int wcd937x_mbhc_hs_detect(struct snd_soc_codec *codec,
  794. struct wcd_mbhc_config *mbhc_cfg)
  795. {
  796. struct wcd937x_priv *wcd937x = NULL;
  797. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  798. if (!codec) {
  799. pr_err("%s: codec is NULL\n", __func__);
  800. return -EINVAL;
  801. }
  802. wcd937x = snd_soc_codec_get_drvdata(codec);
  803. if (!wcd937x) {
  804. pr_err("%s: wcd937x is NULL\n", __func__);
  805. return -EINVAL;
  806. }
  807. wcd937x_mbhc = wcd937x->mbhc;
  808. if (!wcd937x_mbhc) {
  809. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  810. return -EINVAL;
  811. }
  812. return wcd_mbhc_start(&wcd937x_mbhc->wcd_mbhc, mbhc_cfg);
  813. }
  814. EXPORT_SYMBOL(wcd937x_mbhc_hs_detect);
  815. /*
  816. * wcd937x_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  817. * @codec: handle to snd_soc_codec *
  818. */
  819. void wcd937x_mbhc_hs_detect_exit(struct snd_soc_codec *codec)
  820. {
  821. struct wcd937x_priv *wcd937x = NULL;
  822. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  823. if (!codec) {
  824. pr_err("%s: codec is NULL\n", __func__);
  825. return;
  826. }
  827. wcd937x = snd_soc_codec_get_drvdata(codec);
  828. if (!wcd937x) {
  829. pr_err("%s: wcd937x is NULL\n", __func__);
  830. return;
  831. }
  832. wcd937x_mbhc = wcd937x->mbhc;
  833. if (!wcd937x_mbhc) {
  834. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  835. return;
  836. }
  837. wcd_mbhc_stop(&wcd937x_mbhc->wcd_mbhc);
  838. }
  839. EXPORT_SYMBOL(wcd937x_mbhc_hs_detect_exit);
  840. /*
  841. * wcd937x_mbhc_post_ssr_init: initialize mbhc for
  842. * wcd937x post subsystem restart
  843. * @mbhc: poniter to wcd937x_mbhc structure
  844. * @codec: handle to snd_soc_codec *
  845. *
  846. * return 0 if mbhc_init is success or error code in case of failure
  847. */
  848. int wcd937x_mbhc_post_ssr_init(struct wcd937x_mbhc *mbhc,
  849. struct snd_soc_codec *codec)
  850. {
  851. int ret = 0;
  852. struct wcd_mbhc *wcd_mbhc = NULL;
  853. if (!mbhc || !codec)
  854. return -EINVAL;
  855. wcd_mbhc = &mbhc->wcd_mbhc;
  856. if (wcd_mbhc == NULL) {
  857. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  858. return -EINVAL;
  859. }
  860. wcd_mbhc_deinit(wcd_mbhc);
  861. ret = wcd_mbhc_init(wcd_mbhc, codec, &mbhc_cb, &intr_ids,
  862. wcd_mbhc_registers, WCD937X_ZDET_SUPPORTED);
  863. if (ret) {
  864. dev_err(codec->dev, "%s: mbhc initialization failed\n",
  865. __func__);
  866. goto done;
  867. }
  868. done:
  869. return ret;
  870. }
  871. EXPORT_SYMBOL(wcd937x_mbhc_post_ssr_init);
  872. /*
  873. * wcd937x_mbhc_init: initialize mbhc for wcd937x
  874. * @mbhc: poniter to wcd937x_mbhc struct pointer to store the configs
  875. * @codec: handle to snd_soc_codec *
  876. * @fw_data: handle to firmware data
  877. *
  878. * return 0 if mbhc_init is success or error code in case of failure
  879. */
  880. int wcd937x_mbhc_init(struct wcd937x_mbhc **mbhc, struct snd_soc_codec *codec,
  881. struct fw_info *fw_data)
  882. {
  883. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  884. struct wcd_mbhc *wcd_mbhc = NULL;
  885. int ret = 0;
  886. if (!codec) {
  887. pr_err("%s: codec is NULL\n", __func__);
  888. return -EINVAL;
  889. }
  890. wcd937x_mbhc = devm_kzalloc(codec->dev, sizeof(struct wcd937x_mbhc),
  891. GFP_KERNEL);
  892. if (!wcd937x_mbhc)
  893. return -ENOMEM;
  894. wcd937x_mbhc->fw_data = fw_data;
  895. BLOCKING_INIT_NOTIFIER_HEAD(&wcd937x_mbhc->notifier);
  896. wcd_mbhc = &wcd937x_mbhc->wcd_mbhc;
  897. if (wcd_mbhc == NULL) {
  898. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  899. ret = -EINVAL;
  900. goto err;
  901. }
  902. /* Setting default mbhc detection logic to ADC */
  903. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  904. ret = wcd_mbhc_init(wcd_mbhc, codec, &mbhc_cb,
  905. &intr_ids, wcd_mbhc_registers,
  906. WCD937X_ZDET_SUPPORTED);
  907. if (ret) {
  908. dev_err(codec->dev, "%s: mbhc initialization failed\n",
  909. __func__);
  910. goto err;
  911. }
  912. (*mbhc) = wcd937x_mbhc;
  913. snd_soc_add_codec_controls(codec, impedance_detect_controls,
  914. ARRAY_SIZE(impedance_detect_controls));
  915. snd_soc_add_codec_controls(codec, hph_type_detect_controls,
  916. ARRAY_SIZE(hph_type_detect_controls));
  917. return 0;
  918. err:
  919. devm_kfree(codec->dev, wcd937x_mbhc);
  920. return ret;
  921. }
  922. EXPORT_SYMBOL(wcd937x_mbhc_init);
  923. /*
  924. * wcd937x_mbhc_deinit: deinitialize mbhc for wcd937x
  925. * @codec: handle to snd_soc_codec *
  926. */
  927. void wcd937x_mbhc_deinit(struct snd_soc_codec *codec)
  928. {
  929. struct wcd937x_priv *wcd937x;
  930. struct wcd937x_mbhc *wcd937x_mbhc;
  931. if (!codec) {
  932. pr_err("%s: codec is NULL\n", __func__);
  933. return;
  934. }
  935. wcd937x = snd_soc_codec_get_drvdata(codec);
  936. if (!wcd937x) {
  937. pr_err("%s: wcd937x is NULL\n", __func__);
  938. return;
  939. }
  940. wcd937x_mbhc = wcd937x->mbhc;
  941. if (wcd937x_mbhc) {
  942. wcd_mbhc_deinit(&wcd937x_mbhc->wcd_mbhc);
  943. devm_kfree(codec->dev, wcd937x_mbhc);
  944. }
  945. }
  946. EXPORT_SYMBOL(wcd937x_mbhc_deinit);