wcd938x-mbhc.c 33 KB

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