wcd937x-mbhc.c 32 KB

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