wcd937x-mbhc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. // SPDX-License-Identifier: GPL-2.0
  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 "../wcdcal-hwdep.h"
  20. #include "../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. return true;
  225. }
  226. static int wcd937x_mbhc_register_notifier(struct wcd_mbhc *mbhc,
  227. struct notifier_block *nblock,
  228. bool enable)
  229. {
  230. struct wcd937x_mbhc *wcd937x_mbhc;
  231. wcd937x_mbhc = container_of(mbhc, struct wcd937x_mbhc, wcd_mbhc);
  232. if (enable)
  233. return blocking_notifier_chain_register(&wcd937x_mbhc->notifier,
  234. nblock);
  235. else
  236. return blocking_notifier_chain_unregister(
  237. &wcd937x_mbhc->notifier, nblock);
  238. }
  239. static bool wcd937x_mbhc_micb_en_status(struct wcd_mbhc *mbhc, int micb_num)
  240. {
  241. u8 val = 0;
  242. if (micb_num == MIC_BIAS_2) {
  243. val = ((snd_soc_component_read32(mbhc->component,
  244. WCD937X_ANA_MICB2) & 0xC0)
  245. >> 6);
  246. if (val == 0x01)
  247. return true;
  248. }
  249. return false;
  250. }
  251. static bool wcd937x_mbhc_hph_pa_on_status(struct snd_soc_component *component)
  252. {
  253. return (snd_soc_component_read32(component, WCD937X_ANA_HPH) & 0xC0) ?
  254. true : false;
  255. }
  256. static void wcd937x_mbhc_hph_l_pull_up_control(
  257. struct snd_soc_component *component,
  258. int pull_up_cur)
  259. {
  260. /* Default pull up current to 2uA */
  261. if (pull_up_cur > HS_PULLUP_I_OFF || pull_up_cur < HS_PULLUP_I_3P0_UA ||
  262. pull_up_cur == HS_PULLUP_I_DEFAULT)
  263. pull_up_cur = HS_PULLUP_I_2P0_UA;
  264. dev_dbg(component->dev, "%s: HS pull up current:%d\n",
  265. __func__, pull_up_cur);
  266. snd_soc_component_update_bits(component,
  267. WCD937X_MBHC_NEW_INT_MECH_DET_CURRENT,
  268. 0x1F, pull_up_cur);
  269. }
  270. static int wcd937x_mbhc_request_micbias(struct snd_soc_component *component,
  271. int micb_num, int req)
  272. {
  273. int ret = 0;
  274. ret = wcd937x_micbias_control(component, micb_num, req, false);
  275. return ret;
  276. }
  277. static void wcd937x_mbhc_micb_ramp_control(struct snd_soc_component *component,
  278. bool enable)
  279. {
  280. if (enable) {
  281. snd_soc_component_update_bits(component, WCD937X_ANA_MICB2_RAMP,
  282. 0x1C, 0x0C);
  283. snd_soc_component_update_bits(component, WCD937X_ANA_MICB2_RAMP,
  284. 0x80, 0x80);
  285. } else {
  286. snd_soc_component_update_bits(component, WCD937X_ANA_MICB2_RAMP,
  287. 0x80, 0x00);
  288. snd_soc_component_update_bits(component, WCD937X_ANA_MICB2_RAMP,
  289. 0x1C, 0x00);
  290. }
  291. }
  292. static struct firmware_cal *wcd937x_get_hwdep_fw_cal(struct wcd_mbhc *mbhc,
  293. enum wcd_cal_type type)
  294. {
  295. struct wcd937x_mbhc *wcd937x_mbhc;
  296. struct firmware_cal *hwdep_cal;
  297. struct snd_soc_component *component = mbhc->component;
  298. wcd937x_mbhc = container_of(mbhc, struct wcd937x_mbhc, wcd_mbhc);
  299. if (!component) {
  300. pr_err("%s: NULL component pointer\n", __func__);
  301. return NULL;
  302. }
  303. hwdep_cal = wcdcal_get_fw_cal(wcd937x_mbhc->fw_data, type);
  304. if (!hwdep_cal)
  305. dev_err(component->dev, "%s: cal not sent by %d\n",
  306. __func__, type);
  307. return hwdep_cal;
  308. }
  309. static int wcd937x_mbhc_micb_ctrl_threshold_mic(
  310. struct snd_soc_component *component,
  311. int micb_num, bool req_en)
  312. {
  313. struct wcd937x_pdata *pdata = dev_get_platdata(component->dev);
  314. int rc, micb_mv;
  315. if (micb_num != MIC_BIAS_2)
  316. return -EINVAL;
  317. /*
  318. * If device tree micbias level is already above the minimum
  319. * voltage needed to detect threshold microphone, then do
  320. * not change the micbias, just return.
  321. */
  322. if (pdata->micbias.micb2_mv >= WCD_MBHC_THR_HS_MICB_MV)
  323. return 0;
  324. micb_mv = req_en ? WCD_MBHC_THR_HS_MICB_MV : pdata->micbias.micb2_mv;
  325. rc = wcd937x_mbhc_micb_adjust_voltage(component, micb_mv, MIC_BIAS_2);
  326. return rc;
  327. }
  328. static inline void wcd937x_mbhc_get_result_params(struct wcd937x_priv *wcd937x,
  329. s16 *d1_a, u16 noff,
  330. int32_t *zdet)
  331. {
  332. int i;
  333. int val, val1;
  334. s16 c1;
  335. s32 x1, d1;
  336. int32_t denom;
  337. int minCode_param[] = {
  338. 3277, 1639, 820, 410, 205, 103, 52, 26
  339. };
  340. regmap_update_bits(wcd937x->regmap, WCD937X_ANA_MBHC_ZDET, 0x20, 0x20);
  341. for (i = 0; i < WCD937X_ZDET_NUM_MEASUREMENTS; i++) {
  342. regmap_read(wcd937x->regmap, WCD937X_ANA_MBHC_RESULT_2, &val);
  343. if (val & 0x80)
  344. break;
  345. }
  346. val = val << 0x8;
  347. regmap_read(wcd937x->regmap, WCD937X_ANA_MBHC_RESULT_1, &val1);
  348. val |= val1;
  349. regmap_update_bits(wcd937x->regmap, WCD937X_ANA_MBHC_ZDET, 0x20, 0x00);
  350. x1 = WCD937X_MBHC_GET_X1(val);
  351. c1 = WCD937X_MBHC_GET_C1(val);
  352. /* If ramp is not complete, give additional 5ms */
  353. if ((c1 < 2) && x1)
  354. usleep_range(5000, 5050);
  355. if (!c1 || !x1) {
  356. dev_dbg(wcd937x->dev,
  357. "%s: Impedance detect ramp error, c1=%d, x1=0x%x\n",
  358. __func__, c1, x1);
  359. goto ramp_down;
  360. }
  361. d1 = d1_a[c1];
  362. denom = (x1 * d1) - (1 << (14 - noff));
  363. if (denom > 0)
  364. *zdet = (WCD937X_MBHC_ZDET_CONST * 1000) / denom;
  365. else if (x1 < minCode_param[noff])
  366. *zdet = WCD937X_ZDET_FLOATING_IMPEDANCE;
  367. dev_dbg(wcd937x->dev, "%s: d1=%d, c1=%d, x1=0x%x, z_val=%d(milliOhm)\n",
  368. __func__, d1, c1, x1, *zdet);
  369. ramp_down:
  370. i = 0;
  371. while (x1) {
  372. regmap_read(wcd937x->regmap, WCD937X_ANA_MBHC_RESULT_1, &val);
  373. regmap_read(wcd937x->regmap, WCD937X_ANA_MBHC_RESULT_2, &val1);
  374. val = val << 0x8;
  375. val |= val1;
  376. x1 = WCD937X_MBHC_GET_X1(val);
  377. i++;
  378. if (i == WCD937X_ZDET_NUM_MEASUREMENTS)
  379. break;
  380. }
  381. }
  382. static void wcd937x_mbhc_zdet_ramp(struct snd_soc_component *component,
  383. struct wcd937x_mbhc_zdet_param *zdet_param,
  384. int32_t *zl, int32_t *zr, s16 *d1_a)
  385. {
  386. struct wcd937x_priv *wcd937x = dev_get_drvdata(component->dev);
  387. int32_t zdet = 0;
  388. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_ZDET_ANA_CTL,
  389. 0x70, zdet_param->ldo_ctl << 4);
  390. snd_soc_component_update_bits(component, WCD937X_ANA_MBHC_BTN5, 0xFC,
  391. zdet_param->btn5);
  392. snd_soc_component_update_bits(component, WCD937X_ANA_MBHC_BTN6, 0xFC,
  393. zdet_param->btn6);
  394. snd_soc_component_update_bits(component, WCD937X_ANA_MBHC_BTN7, 0xFC,
  395. zdet_param->btn7);
  396. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_ZDET_ANA_CTL,
  397. 0x0F, zdet_param->noff);
  398. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_ZDET_RAMP_CTL,
  399. 0x0F, zdet_param->nshift);
  400. if (!zl)
  401. goto z_right;
  402. /* Start impedance measurement for HPH_L */
  403. regmap_update_bits(wcd937x->regmap,
  404. WCD937X_ANA_MBHC_ZDET, 0x80, 0x80);
  405. dev_dbg(wcd937x->dev, "%s: ramp for HPH_L, noff = %d\n",
  406. __func__, zdet_param->noff);
  407. wcd937x_mbhc_get_result_params(wcd937x, d1_a, zdet_param->noff, &zdet);
  408. regmap_update_bits(wcd937x->regmap,
  409. WCD937X_ANA_MBHC_ZDET, 0x80, 0x00);
  410. *zl = zdet;
  411. z_right:
  412. if (!zr)
  413. return;
  414. /* Start impedance measurement for HPH_R */
  415. regmap_update_bits(wcd937x->regmap,
  416. WCD937X_ANA_MBHC_ZDET, 0x40, 0x40);
  417. dev_dbg(wcd937x->dev, "%s: ramp for HPH_R, noff = %d\n",
  418. __func__, zdet_param->noff);
  419. wcd937x_mbhc_get_result_params(wcd937x, d1_a, zdet_param->noff, &zdet);
  420. regmap_update_bits(wcd937x->regmap,
  421. WCD937X_ANA_MBHC_ZDET, 0x40, 0x00);
  422. *zr = zdet;
  423. }
  424. static inline void wcd937x_wcd_mbhc_qfuse_cal(
  425. struct snd_soc_component *component,
  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_component_read32(component,
  432. WCD937X_DIGITAL_EFUSE_REG_23 + (2 * flag_l_r));
  433. else
  434. q1 = snd_soc_component_read32(component,
  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_component *component = mbhc->component;
  447. struct wcd937x_priv *wcd937x = dev_get_drvdata(component->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_component_read32(component, WCD937X_ANA_MBHC_BTN5);
  468. reg1 = snd_soc_component_read32(component, WCD937X_ANA_MBHC_BTN6);
  469. reg2 = snd_soc_component_read32(component, WCD937X_ANA_MBHC_BTN7);
  470. reg3 = snd_soc_component_read32(component, WCD937X_MBHC_CTL_CLK);
  471. reg4 = snd_soc_component_read32(component,
  472. WCD937X_MBHC_NEW_ZDET_ANA_CTL);
  473. if (snd_soc_component_read32(component, WCD937X_ANA_MBHC_ELECT) &
  474. 0x80) {
  475. is_fsm_disable = true;
  476. regmap_update_bits(wcd937x->regmap,
  477. WCD937X_ANA_MBHC_ELECT, 0x80, 0x00);
  478. }
  479. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  480. if (mbhc->hphl_swh)
  481. regmap_update_bits(wcd937x->regmap,
  482. WCD937X_ANA_MBHC_MECH, 0x80, 0x00);
  483. /* Turn off 100k pull down on HPHL */
  484. regmap_update_bits(wcd937x->regmap,
  485. WCD937X_ANA_MBHC_MECH, 0x01, 0x00);
  486. /* First get impedance on Left */
  487. d1 = d1_a[1];
  488. zdet_param_ptr = &zdet_param[1];
  489. wcd937x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  490. if (!WCD937X_MBHC_IS_SECOND_RAMP_REQUIRED(z1L))
  491. goto left_ch_impedance;
  492. /* Second ramp for left ch */
  493. if (z1L < WCD937X_ZDET_VAL_32) {
  494. zdet_param_ptr = &zdet_param[0];
  495. d1 = d1_a[0];
  496. } else if ((z1L > WCD937X_ZDET_VAL_400) &&
  497. (z1L <= WCD937X_ZDET_VAL_1200)) {
  498. zdet_param_ptr = &zdet_param[2];
  499. d1 = d1_a[2];
  500. } else if (z1L > WCD937X_ZDET_VAL_1200) {
  501. zdet_param_ptr = &zdet_param[3];
  502. d1 = d1_a[3];
  503. }
  504. wcd937x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  505. left_ch_impedance:
  506. if ((z1L == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  507. (z1L > WCD937X_ZDET_VAL_100K)) {
  508. *zl = WCD937X_ZDET_FLOATING_IMPEDANCE;
  509. zdet_param_ptr = &zdet_param[1];
  510. d1 = d1_a[1];
  511. } else {
  512. *zl = z1L/1000;
  513. wcd937x_wcd_mbhc_qfuse_cal(component, zl, 0);
  514. }
  515. dev_dbg(component->dev, "%s: impedance on HPH_L = %d(ohms)\n",
  516. __func__, *zl);
  517. /* Start of right impedance ramp and calculation */
  518. wcd937x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
  519. if (WCD937X_MBHC_IS_SECOND_RAMP_REQUIRED(z1R)) {
  520. if (((z1R > WCD937X_ZDET_VAL_1200) &&
  521. (zdet_param_ptr->noff == 0x6)) ||
  522. ((*zl) != WCD937X_ZDET_FLOATING_IMPEDANCE))
  523. goto right_ch_impedance;
  524. /* Second ramp for right ch */
  525. if (z1R < WCD937X_ZDET_VAL_32) {
  526. zdet_param_ptr = &zdet_param[0];
  527. d1 = d1_a[0];
  528. } else if ((z1R > WCD937X_ZDET_VAL_400) &&
  529. (z1R <= WCD937X_ZDET_VAL_1200)) {
  530. zdet_param_ptr = &zdet_param[2];
  531. d1 = d1_a[2];
  532. } else if (z1R > WCD937X_ZDET_VAL_1200) {
  533. zdet_param_ptr = &zdet_param[3];
  534. d1 = d1_a[3];
  535. }
  536. wcd937x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL,
  537. &z1R, d1);
  538. }
  539. right_ch_impedance:
  540. if ((z1R == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  541. (z1R > WCD937X_ZDET_VAL_100K)) {
  542. *zr = WCD937X_ZDET_FLOATING_IMPEDANCE;
  543. } else {
  544. *zr = z1R/1000;
  545. wcd937x_wcd_mbhc_qfuse_cal(component, zr, 1);
  546. }
  547. dev_dbg(component->dev, "%s: impedance on HPH_R = %d(ohms)\n",
  548. __func__, *zr);
  549. /* Mono/stereo detection */
  550. if ((*zl == WCD937X_ZDET_FLOATING_IMPEDANCE) &&
  551. (*zr == WCD937X_ZDET_FLOATING_IMPEDANCE)) {
  552. dev_dbg(component->dev,
  553. "%s: plug type is invalid or extension cable\n",
  554. __func__);
  555. goto zdet_complete;
  556. }
  557. if ((*zl == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  558. (*zr == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  559. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  560. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  561. dev_dbg(component->dev,
  562. "%s: Mono plug type with one ch floating or shorted to GND\n",
  563. __func__);
  564. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  565. goto zdet_complete;
  566. }
  567. snd_soc_component_update_bits(component, WCD937X_HPH_R_ATEST,
  568. 0x02, 0x02);
  569. snd_soc_component_update_bits(component, WCD937X_HPH_PA_CTL2,
  570. 0x40, 0x01);
  571. if (*zl < (WCD937X_ZDET_VAL_32/1000))
  572. wcd937x_mbhc_zdet_ramp(component, &zdet_param[0], &z1Ls,
  573. NULL, d1);
  574. else
  575. wcd937x_mbhc_zdet_ramp(component, &zdet_param[1], &z1Ls,
  576. NULL, d1);
  577. snd_soc_component_update_bits(component, WCD937X_HPH_PA_CTL2,
  578. 0x40, 0x00);
  579. snd_soc_component_update_bits(component, WCD937X_HPH_R_ATEST,
  580. 0x02, 0x00);
  581. z1Ls /= 1000;
  582. wcd937x_wcd_mbhc_qfuse_cal(component, &z1Ls, 0);
  583. /* Parallel of left Z and 9 ohm pull down resistor */
  584. zMono = ((*zl) * 9) / ((*zl) + 9);
  585. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  586. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  587. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  588. dev_dbg(component->dev, "%s: stereo plug type detected\n",
  589. __func__);
  590. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  591. } else {
  592. dev_dbg(component->dev, "%s: MONO plug type detected\n",
  593. __func__);
  594. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  595. }
  596. zdet_complete:
  597. snd_soc_component_write(component, WCD937X_ANA_MBHC_BTN5, reg0);
  598. snd_soc_component_write(component, WCD937X_ANA_MBHC_BTN6, reg1);
  599. snd_soc_component_write(component, WCD937X_ANA_MBHC_BTN7, reg2);
  600. /* Turn on 100k pull down on HPHL */
  601. regmap_update_bits(wcd937x->regmap,
  602. WCD937X_ANA_MBHC_MECH, 0x01, 0x01);
  603. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  604. if (mbhc->hphl_swh)
  605. regmap_update_bits(wcd937x->regmap,
  606. WCD937X_ANA_MBHC_MECH, 0x80, 0x80);
  607. snd_soc_component_write(component, WCD937X_MBHC_NEW_ZDET_ANA_CTL, reg4);
  608. snd_soc_component_write(component, WCD937X_MBHC_CTL_CLK, reg3);
  609. if (is_fsm_disable)
  610. regmap_update_bits(wcd937x->regmap,
  611. WCD937X_ANA_MBHC_ELECT, 0x80, 0x80);
  612. }
  613. static void wcd937x_mbhc_gnd_det_ctrl(struct snd_soc_component *component,
  614. bool enable)
  615. {
  616. if (enable) {
  617. snd_soc_component_update_bits(component, WCD937X_ANA_MBHC_MECH,
  618. 0x02, 0x02);
  619. snd_soc_component_update_bits(component, WCD937X_ANA_MBHC_MECH,
  620. 0x40, 0x40);
  621. } else {
  622. snd_soc_component_update_bits(component, WCD937X_ANA_MBHC_MECH,
  623. 0x40, 0x00);
  624. snd_soc_component_update_bits(component, WCD937X_ANA_MBHC_MECH,
  625. 0x02, 0x00);
  626. }
  627. }
  628. static void wcd937x_mbhc_hph_pull_down_ctrl(struct snd_soc_component *component,
  629. bool enable)
  630. {
  631. if (enable) {
  632. snd_soc_component_update_bits(component, WCD937X_HPH_PA_CTL2,
  633. 0x40, 0x40);
  634. snd_soc_component_update_bits(component, WCD937X_HPH_PA_CTL2,
  635. 0x10, 0x10);
  636. } else {
  637. snd_soc_component_update_bits(component, WCD937X_HPH_PA_CTL2,
  638. 0x40, 0x00);
  639. snd_soc_component_update_bits(component, WCD937X_HPH_PA_CTL2,
  640. 0x10, 0x00);
  641. }
  642. }
  643. static void wcd937x_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  644. {
  645. struct snd_soc_component *component = mbhc->component;
  646. if ((mbhc->moist_rref == R_OFF) ||
  647. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  648. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_CTL_2,
  649. 0x0C, R_OFF << 2);
  650. return;
  651. }
  652. /* Do not enable moisture detection if jack type is NC */
  653. if (!mbhc->hphl_swh) {
  654. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  655. __func__);
  656. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_CTL_2,
  657. 0x0C, R_OFF << 2);
  658. return;
  659. }
  660. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_CTL_2,
  661. 0x0C, mbhc->moist_rref << 2);
  662. }
  663. static void wcd937x_mbhc_moisture_detect_en(struct wcd_mbhc *mbhc, bool enable)
  664. {
  665. struct snd_soc_component *component = mbhc->component;
  666. if (enable)
  667. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_CTL_2,
  668. 0x0C, mbhc->moist_rref << 2);
  669. else
  670. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_CTL_2,
  671. 0x0C, R_OFF << 2);
  672. }
  673. static bool wcd937x_mbhc_get_moisture_status(struct wcd_mbhc *mbhc)
  674. {
  675. struct snd_soc_component *component = mbhc->component;
  676. bool ret = false;
  677. if ((mbhc->moist_rref == R_OFF) ||
  678. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  679. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_CTL_2,
  680. 0x0C, R_OFF << 2);
  681. goto done;
  682. }
  683. /* Do not enable moisture detection if jack type is NC */
  684. if (!mbhc->hphl_swh) {
  685. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  686. __func__);
  687. snd_soc_component_update_bits(component, WCD937X_MBHC_NEW_CTL_2,
  688. 0x0C, R_OFF << 2);
  689. goto done;
  690. }
  691. /* If moisture_en is already enabled, then skip to plug type
  692. * detection.
  693. */
  694. if ((snd_soc_component_read32(component, WCD937X_MBHC_NEW_CTL_2) &
  695. 0x0C))
  696. goto done;
  697. wcd937x_mbhc_moisture_detect_en(mbhc, true);
  698. /* Read moisture comparator status */
  699. ret = ((snd_soc_component_read32(component, WCD937X_MBHC_NEW_FSM_STATUS)
  700. & 0x20) ? 0 : 1);
  701. done:
  702. return ret;
  703. }
  704. static void wcd937x_mbhc_moisture_polling_ctrl(struct wcd_mbhc *mbhc,
  705. bool enable)
  706. {
  707. struct snd_soc_component *component = mbhc->component;
  708. snd_soc_component_update_bits(component,
  709. WCD937X_MBHC_NEW_INT_MOISTURE_DET_POLLING_CTRL,
  710. 0x04, (enable << 2));
  711. }
  712. static const struct wcd_mbhc_cb mbhc_cb = {
  713. .request_irq = wcd937x_mbhc_request_irq,
  714. .irq_control = wcd937x_mbhc_irq_control,
  715. .free_irq = wcd937x_mbhc_free_irq,
  716. .clk_setup = wcd937x_mbhc_clk_setup,
  717. .map_btn_code_to_num = wcd937x_mbhc_btn_to_num,
  718. .mbhc_bias = wcd937x_mbhc_mbhc_bias_control,
  719. .set_btn_thr = wcd937x_mbhc_program_btn_thr,
  720. .lock_sleep = wcd937x_mbhc_lock_sleep,
  721. .register_notifier = wcd937x_mbhc_register_notifier,
  722. .micbias_enable_status = wcd937x_mbhc_micb_en_status,
  723. .hph_pa_on_status = wcd937x_mbhc_hph_pa_on_status,
  724. .hph_pull_up_control_v2 = wcd937x_mbhc_hph_l_pull_up_control,
  725. .mbhc_micbias_control = wcd937x_mbhc_request_micbias,
  726. .mbhc_micb_ramp_control = wcd937x_mbhc_micb_ramp_control,
  727. .get_hwdep_fw_cal = wcd937x_get_hwdep_fw_cal,
  728. .mbhc_micb_ctrl_thr_mic = wcd937x_mbhc_micb_ctrl_threshold_mic,
  729. .compute_impedance = wcd937x_wcd_mbhc_calc_impedance,
  730. .mbhc_gnd_det_ctrl = wcd937x_mbhc_gnd_det_ctrl,
  731. .hph_pull_down_ctrl = wcd937x_mbhc_hph_pull_down_ctrl,
  732. .mbhc_moisture_config = wcd937x_mbhc_moisture_config,
  733. .mbhc_get_moisture_status = wcd937x_mbhc_get_moisture_status,
  734. .mbhc_moisture_polling_ctrl = wcd937x_mbhc_moisture_polling_ctrl,
  735. .mbhc_moisture_detect_en = wcd937x_mbhc_moisture_detect_en,
  736. };
  737. static int wcd937x_get_hph_type(struct snd_kcontrol *kcontrol,
  738. struct snd_ctl_elem_value *ucontrol)
  739. {
  740. struct snd_soc_component *component =
  741. snd_soc_kcontrol_component(kcontrol);
  742. struct wcd937x_mbhc *wcd937x_mbhc = wcd937x_soc_get_mbhc(component);
  743. struct wcd_mbhc *mbhc;
  744. if (!wcd937x_mbhc) {
  745. dev_err(component->dev, "%s: mbhc not initialized!\n",
  746. __func__);
  747. return -EINVAL;
  748. }
  749. mbhc = &wcd937x_mbhc->wcd_mbhc;
  750. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  751. dev_dbg(component->dev, "%s: hph_type = %u\n", __func__,
  752. mbhc->hph_type);
  753. return 0;
  754. }
  755. static int wcd937x_hph_impedance_get(struct snd_kcontrol *kcontrol,
  756. struct snd_ctl_elem_value *ucontrol)
  757. {
  758. uint32_t zl, zr;
  759. bool hphr;
  760. struct soc_multi_mixer_control *mc;
  761. struct snd_soc_component *component =
  762. snd_soc_kcontrol_component(kcontrol);
  763. struct wcd937x_mbhc *wcd937x_mbhc = wcd937x_soc_get_mbhc(component);
  764. if (!wcd937x_mbhc) {
  765. dev_err(component->dev, "%s: mbhc not initialized!\n",
  766. __func__);
  767. return -EINVAL;
  768. }
  769. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  770. hphr = mc->shift;
  771. wcd_mbhc_get_impedance(&wcd937x_mbhc->wcd_mbhc, &zl, &zr);
  772. dev_dbg(component->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__,
  773. zl, zr);
  774. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  775. return 0;
  776. }
  777. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  778. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  779. wcd937x_get_hph_type, NULL),
  780. };
  781. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  782. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  783. wcd937x_hph_impedance_get, NULL),
  784. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  785. wcd937x_hph_impedance_get, NULL),
  786. };
  787. /*
  788. * wcd937x_mbhc_get_impedance: get impedance of headphone
  789. * left and right channels
  790. * @wcd937x_mbhc: handle to struct wcd937x_mbhc *
  791. * @zl: handle to left-ch impedance
  792. * @zr: handle to right-ch impedance
  793. * return 0 for success or error code in case of failure
  794. */
  795. int wcd937x_mbhc_get_impedance(struct wcd937x_mbhc *wcd937x_mbhc,
  796. uint32_t *zl, uint32_t *zr)
  797. {
  798. if (!wcd937x_mbhc) {
  799. pr_err("%s: mbhc not initialized!\n", __func__);
  800. return -EINVAL;
  801. }
  802. if (!zl || !zr) {
  803. pr_err("%s: zl or zr null!\n", __func__);
  804. return -EINVAL;
  805. }
  806. return wcd_mbhc_get_impedance(&wcd937x_mbhc->wcd_mbhc, zl, zr);
  807. }
  808. EXPORT_SYMBOL(wcd937x_mbhc_get_impedance);
  809. /*
  810. * wcd937x_mbhc_hs_detect: starts mbhc insertion/removal functionality
  811. * @component: handle to snd_soc_component *
  812. * @mbhc_cfg: handle to mbhc configuration structure
  813. * return 0 if mbhc_start is success or error code in case of failure
  814. */
  815. int wcd937x_mbhc_hs_detect(struct snd_soc_component *component,
  816. struct wcd_mbhc_config *mbhc_cfg)
  817. {
  818. struct wcd937x_priv *wcd937x = NULL;
  819. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  820. if (!component) {
  821. pr_err("%s: component is NULL\n", __func__);
  822. return -EINVAL;
  823. }
  824. wcd937x = snd_soc_component_get_drvdata(component);
  825. if (!wcd937x) {
  826. pr_err("%s: wcd937x is NULL\n", __func__);
  827. return -EINVAL;
  828. }
  829. wcd937x_mbhc = wcd937x->mbhc;
  830. if (!wcd937x_mbhc) {
  831. dev_err(component->dev, "%s: mbhc not initialized!\n",
  832. __func__);
  833. return -EINVAL;
  834. }
  835. return wcd_mbhc_start(&wcd937x_mbhc->wcd_mbhc, mbhc_cfg);
  836. }
  837. EXPORT_SYMBOL(wcd937x_mbhc_hs_detect);
  838. /*
  839. * wcd937x_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  840. * @component: handle to snd_soc_component *
  841. */
  842. void wcd937x_mbhc_hs_detect_exit(struct snd_soc_component *component)
  843. {
  844. struct wcd937x_priv *wcd937x = NULL;
  845. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  846. if (!component) {
  847. pr_err("%s: component is NULL\n", __func__);
  848. return;
  849. }
  850. wcd937x = snd_soc_component_get_drvdata(component);
  851. if (!wcd937x) {
  852. pr_err("%s: wcd937x is NULL\n", __func__);
  853. return;
  854. }
  855. wcd937x_mbhc = wcd937x->mbhc;
  856. if (!wcd937x_mbhc) {
  857. dev_err(component->dev, "%s: mbhc not initialized!\n",
  858. __func__);
  859. return;
  860. }
  861. wcd_mbhc_stop(&wcd937x_mbhc->wcd_mbhc);
  862. }
  863. EXPORT_SYMBOL(wcd937x_mbhc_hs_detect_exit);
  864. /*
  865. * wcd937x_mbhc_post_ssr_init: initialize mbhc for
  866. * wcd937x post subsystem restart
  867. * @mbhc: poniter to wcd937x_mbhc structure
  868. * @component: handle to snd_soc_component *
  869. *
  870. * return 0 if mbhc_init is success or error code in case of failure
  871. */
  872. int wcd937x_mbhc_post_ssr_init(struct wcd937x_mbhc *mbhc,
  873. struct snd_soc_component *component)
  874. {
  875. int ret = 0;
  876. struct wcd_mbhc *wcd_mbhc = NULL;
  877. if (!mbhc || !component)
  878. return -EINVAL;
  879. wcd_mbhc = &mbhc->wcd_mbhc;
  880. if (wcd_mbhc == NULL) {
  881. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  882. return -EINVAL;
  883. }
  884. wcd937x_mbhc_hs_detect_exit(component);
  885. wcd_mbhc_deinit(wcd_mbhc);
  886. snd_soc_component_update_bits(component, WCD937X_ANA_MBHC_MECH,
  887. 0x20, 0x20);
  888. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb, &intr_ids,
  889. wcd_mbhc_registers, WCD937X_ZDET_SUPPORTED);
  890. if (ret) {
  891. dev_err(component->dev, "%s: mbhc initialization failed\n",
  892. __func__);
  893. goto done;
  894. }
  895. done:
  896. return ret;
  897. }
  898. EXPORT_SYMBOL(wcd937x_mbhc_post_ssr_init);
  899. /*
  900. * wcd937x_mbhc_init: initialize mbhc for wcd937x
  901. * @mbhc: poniter to wcd937x_mbhc struct pointer to store the configs
  902. * @component: handle to snd_soc_component *
  903. * @fw_data: handle to firmware data
  904. *
  905. * return 0 if mbhc_init is success or error code in case of failure
  906. */
  907. int wcd937x_mbhc_init(struct wcd937x_mbhc **mbhc,
  908. struct snd_soc_component *component,
  909. struct fw_info *fw_data)
  910. {
  911. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  912. struct wcd_mbhc *wcd_mbhc = NULL;
  913. int ret = 0;
  914. if (!component) {
  915. pr_err("%s: component is NULL\n", __func__);
  916. return -EINVAL;
  917. }
  918. wcd937x_mbhc = devm_kzalloc(component->dev, sizeof(struct wcd937x_mbhc),
  919. GFP_KERNEL);
  920. if (!wcd937x_mbhc)
  921. return -ENOMEM;
  922. wcd937x_mbhc->fw_data = fw_data;
  923. BLOCKING_INIT_NOTIFIER_HEAD(&wcd937x_mbhc->notifier);
  924. wcd_mbhc = &wcd937x_mbhc->wcd_mbhc;
  925. if (wcd_mbhc == NULL) {
  926. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  927. ret = -EINVAL;
  928. goto err;
  929. }
  930. /* Setting default mbhc detection logic to ADC */
  931. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  932. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb,
  933. &intr_ids, wcd_mbhc_registers,
  934. WCD937X_ZDET_SUPPORTED);
  935. if (ret) {
  936. dev_err(component->dev, "%s: mbhc initialization failed\n",
  937. __func__);
  938. goto err;
  939. }
  940. (*mbhc) = wcd937x_mbhc;
  941. snd_soc_add_component_controls(component, impedance_detect_controls,
  942. ARRAY_SIZE(impedance_detect_controls));
  943. snd_soc_add_component_controls(component, hph_type_detect_controls,
  944. ARRAY_SIZE(hph_type_detect_controls));
  945. return 0;
  946. err:
  947. devm_kfree(component->dev, wcd937x_mbhc);
  948. return ret;
  949. }
  950. EXPORT_SYMBOL(wcd937x_mbhc_init);
  951. /*
  952. * wcd937x_mbhc_deinit: deinitialize mbhc for wcd937x
  953. * @component: handle to snd_soc_component *
  954. */
  955. void wcd937x_mbhc_deinit(struct snd_soc_component *component)
  956. {
  957. struct wcd937x_priv *wcd937x;
  958. struct wcd937x_mbhc *wcd937x_mbhc;
  959. if (!component) {
  960. pr_err("%s: component is NULL\n", __func__);
  961. return;
  962. }
  963. wcd937x = snd_soc_component_get_drvdata(component);
  964. if (!wcd937x) {
  965. pr_err("%s: wcd937x is NULL\n", __func__);
  966. return;
  967. }
  968. wcd937x_mbhc = wcd937x->mbhc;
  969. if (wcd937x_mbhc) {
  970. wcd_mbhc_deinit(&wcd937x_mbhc->wcd_mbhc);
  971. devm_kfree(component->dev, wcd937x_mbhc);
  972. }
  973. }
  974. EXPORT_SYMBOL(wcd937x_mbhc_deinit);