wcd937x-mbhc.c 30 KB

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