aqt1000-mbhc.c 31 KB

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