wcd938x-mbhc.c 34 KB

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