wcd937x-mbhc.c 33 KB

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