wcd937x-mbhc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /* Copyright (c) 2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/device.h>
  16. #include <linux/printk.h>
  17. #include <linux/ratelimit.h>
  18. #include <linux/kernel.h>
  19. #include <linux/gpio.h>
  20. #include <linux/delay.h>
  21. #include <linux/regmap.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include "wcd937x-registers.h"
  27. #include "../wcdcal-hwdep.h"
  28. #include "../wcd-mbhc-v2-api.h"
  29. #include "internal.h"
  30. #define WCD937X_ZDET_SUPPORTED true
  31. /* Z value defined in milliohm */
  32. #define WCD937X_ZDET_VAL_32 32000
  33. #define WCD937X_ZDET_VAL_400 400000
  34. #define WCD937X_ZDET_VAL_1200 1200000
  35. #define WCD937X_ZDET_VAL_100K 100000000
  36. /* Z floating defined in ohms */
  37. #define WCD937X_ZDET_FLOATING_IMPEDANCE 0x0FFFFFFE
  38. #define WCD937X_ZDET_NUM_MEASUREMENTS 900
  39. #define WCD937X_MBHC_GET_C1(c) ((c & 0xC000) >> 14)
  40. #define WCD937X_MBHC_GET_X1(x) (x & 0x3FFF)
  41. /* Z value compared in milliOhm */
  42. #define WCD937X_MBHC_IS_SECOND_RAMP_REQUIRED(z) ((z > 400000) || (z < 32000))
  43. #define WCD937X_MBHC_ZDET_CONST (86 * 16384)
  44. #define WCD937X_MBHC_MOISTURE_RREF R_24_KOHM
  45. static struct wcd_mbhc_register
  46. wcd_mbhc_registers[WCD_MBHC_REG_FUNC_MAX] = {
  47. WCD_MBHC_REGISTER("WCD_MBHC_L_DET_EN",
  48. WCD937X_ANA_MBHC_MECH, 0x80, 7, 0),
  49. WCD_MBHC_REGISTER("WCD_MBHC_GND_DET_EN",
  50. WCD937X_ANA_MBHC_MECH, 0x40, 6, 0),
  51. WCD_MBHC_REGISTER("WCD_MBHC_MECH_DETECTION_TYPE",
  52. WCD937X_ANA_MBHC_MECH, 0x20, 5, 0),
  53. WCD_MBHC_REGISTER("WCD_MBHC_MIC_CLAMP_CTL",
  54. WCD937X_MBHC_NEW_PLUG_DETECT_CTL, 0x30, 4, 0),
  55. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_DETECTION_TYPE",
  56. WCD937X_ANA_MBHC_ELECT, 0x08, 3, 0),
  57. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_CTRL",
  58. WCD937X_MBHC_NEW_INT_MECH_DET_CURRENT, 0x1F, 0, 0),
  59. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL",
  60. WCD937X_ANA_MBHC_MECH, 0x04, 2, 0),
  61. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PLUG_TYPE",
  62. WCD937X_ANA_MBHC_MECH, 0x10, 4, 0),
  63. WCD_MBHC_REGISTER("WCD_MBHC_GND_PLUG_TYPE",
  64. WCD937X_ANA_MBHC_MECH, 0x08, 3, 0),
  65. WCD_MBHC_REGISTER("WCD_MBHC_SW_HPH_LP_100K_TO_GND",
  66. WCD937X_ANA_MBHC_MECH, 0x01, 0, 0),
  67. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_SCHMT_ISRC",
  68. WCD937X_ANA_MBHC_ELECT, 0x06, 1, 0),
  69. WCD_MBHC_REGISTER("WCD_MBHC_FSM_EN",
  70. WCD937X_ANA_MBHC_ELECT, 0x80, 7, 0),
  71. WCD_MBHC_REGISTER("WCD_MBHC_INSREM_DBNC",
  72. WCD937X_MBHC_NEW_PLUG_DETECT_CTL, 0x0F, 0, 0),
  73. WCD_MBHC_REGISTER("WCD_MBHC_BTN_DBNC",
  74. WCD937X_MBHC_NEW_CTL_1, 0x03, 0, 0),
  75. WCD_MBHC_REGISTER("WCD_MBHC_HS_VREF",
  76. WCD937X_MBHC_NEW_CTL_2, 0x03, 0, 0),
  77. WCD_MBHC_REGISTER("WCD_MBHC_HS_COMP_RESULT",
  78. WCD937X_ANA_MBHC_RESULT_3, 0x08, 3, 0),
  79. WCD_MBHC_REGISTER("WCD_MBHC_MIC_SCHMT_RESULT",
  80. WCD937X_ANA_MBHC_RESULT_3, 0x20, 5, 0),
  81. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_SCHMT_RESULT",
  82. WCD937X_ANA_MBHC_RESULT_3, 0x80, 7, 0),
  83. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_SCHMT_RESULT",
  84. WCD937X_ANA_MBHC_RESULT_3, 0x40, 6, 0),
  85. WCD_MBHC_REGISTER("WCD_MBHC_OCP_FSM_EN",
  86. WCD937X_HPH_OCP_CTL, 0x10, 4, 0),
  87. WCD_MBHC_REGISTER("WCD_MBHC_BTN_RESULT",
  88. WCD937X_ANA_MBHC_RESULT_3, 0x07, 0, 0),
  89. WCD_MBHC_REGISTER("WCD_MBHC_BTN_ISRC_CTL",
  90. WCD937X_ANA_MBHC_ELECT, 0x70, 4, 0),
  91. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_RESULT",
  92. WCD937X_ANA_MBHC_RESULT_3, 0xFF, 0, 0),
  93. WCD_MBHC_REGISTER("WCD_MBHC_MICB_CTRL",
  94. WCD937X_ANA_MICB2, 0xC0, 6, 0),
  95. WCD_MBHC_REGISTER("WCD_MBHC_HPH_CNP_WG_TIME",
  96. WCD937X_HPH_CNP_WG_TIME, 0xFF, 0, 0),
  97. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_PA_EN",
  98. WCD937X_ANA_HPH, 0x40, 6, 0),
  99. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PA_EN",
  100. WCD937X_ANA_HPH, 0x80, 7, 0),
  101. WCD_MBHC_REGISTER("WCD_MBHC_HPH_PA_EN",
  102. WCD937X_ANA_HPH, 0xC0, 6, 0),
  103. WCD_MBHC_REGISTER("WCD_MBHC_SWCH_LEVEL_REMOVE",
  104. WCD937X_ANA_MBHC_RESULT_3, 0x10, 4, 0),
  105. WCD_MBHC_REGISTER("WCD_MBHC_PULLDOWN_CTRL",
  106. 0, 0, 0, 0),
  107. WCD_MBHC_REGISTER("WCD_MBHC_ANC_DET_EN",
  108. WCD937X_MBHC_CTL_BCS, 0x02, 1, 0),
  109. WCD_MBHC_REGISTER("WCD_MBHC_FSM_STATUS",
  110. WCD937X_MBHC_NEW_FSM_STATUS, 0x01, 0, 0),
  111. WCD_MBHC_REGISTER("WCD_MBHC_MUX_CTL",
  112. WCD937X_MBHC_NEW_CTL_2, 0x70, 4, 0),
  113. WCD_MBHC_REGISTER("WCD_MBHC_MOISTURE_STATUS",
  114. WCD937X_MBHC_NEW_FSM_STATUS, 0x20, 5, 0),
  115. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_GND",
  116. WCD937X_HPH_PA_CTL2, 0x40, 6, 0),
  117. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_GND",
  118. WCD937X_HPH_PA_CTL2, 0x10, 4, 0),
  119. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_DET_EN",
  120. WCD937X_HPH_L_TEST, 0x01, 0, 0),
  121. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_DET_EN",
  122. WCD937X_HPH_R_TEST, 0x01, 0, 0),
  123. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_STATUS",
  124. WCD937X_DIGITAL_INTR_STATUS_0, 0x80, 7, 0),
  125. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_STATUS",
  126. WCD937X_DIGITAL_INTR_STATUS_0, 0x20, 5, 0),
  127. WCD_MBHC_REGISTER("WCD_MBHC_ADC_EN",
  128. WCD937X_MBHC_NEW_CTL_1, 0x08, 3, 0),
  129. WCD_MBHC_REGISTER("WCD_MBHC_ADC_COMPLETE", WCD937X_MBHC_NEW_FSM_STATUS,
  130. 0x40, 6, 0),
  131. WCD_MBHC_REGISTER("WCD_MBHC_ADC_TIMEOUT", WCD937X_MBHC_NEW_FSM_STATUS,
  132. 0x80, 7, 0),
  133. WCD_MBHC_REGISTER("WCD_MBHC_ADC_RESULT", WCD937X_MBHC_NEW_ADC_RESULT,
  134. 0xFF, 0, 0),
  135. WCD_MBHC_REGISTER("WCD_MBHC_MICB2_VOUT", WCD937X_ANA_MICB1, 0x3F, 0, 0),
  136. WCD_MBHC_REGISTER("WCD_MBHC_ADC_MODE",
  137. WCD937X_MBHC_NEW_CTL_1, 0x10, 4, 0),
  138. WCD_MBHC_REGISTER("WCD_MBHC_DETECTION_DONE",
  139. WCD937X_MBHC_NEW_CTL_1, 0x04, 2, 0),
  140. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_ISRC_EN",
  141. WCD937X_ANA_MBHC_ZDET, 0x02, 1, 0),
  142. };
  143. static const struct wcd_mbhc_intr intr_ids = {
  144. .mbhc_sw_intr = WCD937X_IRQ_MBHC_SW_DET,
  145. .mbhc_btn_press_intr = WCD937X_IRQ_MBHC_BUTTON_PRESS_DET,
  146. .mbhc_btn_release_intr = WCD937X_IRQ_MBHC_BUTTON_RELEASE_DET,
  147. .mbhc_hs_ins_intr = WCD937X_IRQ_MBHC_ELECT_INS_REM_LEG_DET,
  148. .mbhc_hs_rem_intr = WCD937X_IRQ_MBHC_ELECT_INS_REM_DET,
  149. .hph_left_ocp = WCD937X_IRQ_HPHL_OCP_INT,
  150. .hph_right_ocp = WCD937X_IRQ_HPHR_OCP_INT,
  151. };
  152. struct wcd937x_mbhc_zdet_param {
  153. u16 ldo_ctl;
  154. u16 noff;
  155. u16 nshift;
  156. u16 btn5;
  157. u16 btn6;
  158. u16 btn7;
  159. };
  160. static int wcd937x_mbhc_request_irq(struct snd_soc_codec *codec,
  161. int irq, irq_handler_t handler,
  162. const char *name, void *data)
  163. {
  164. struct wcd937x_priv *wcd937x = dev_get_drvdata(codec->dev);
  165. return wcd_request_irq(&wcd937x->irq_info, irq, name, handler, data);
  166. }
  167. static void wcd937x_mbhc_irq_control(struct snd_soc_codec *codec,
  168. int irq, bool enable)
  169. {
  170. struct wcd937x_priv *wcd937x = dev_get_drvdata(codec->dev);
  171. if (enable)
  172. wcd_enable_irq(&wcd937x->irq_info, irq);
  173. else
  174. wcd_disable_irq(&wcd937x->irq_info, irq);
  175. }
  176. static int wcd937x_mbhc_free_irq(struct snd_soc_codec *codec,
  177. int irq, void *data)
  178. {
  179. struct wcd937x_priv *wcd937x = dev_get_drvdata(codec->dev);
  180. wcd_free_irq(&wcd937x->irq_info, irq, data);
  181. return 0;
  182. }
  183. static void wcd937x_mbhc_clk_setup(struct snd_soc_codec *codec,
  184. bool enable)
  185. {
  186. if (enable)
  187. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_1,
  188. 0x80, 0x80);
  189. else
  190. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_1,
  191. 0x80, 0x00);
  192. }
  193. static int wcd937x_mbhc_btn_to_num(struct snd_soc_codec *codec)
  194. {
  195. return snd_soc_read(codec, WCD937X_ANA_MBHC_RESULT_3) & 0x7;
  196. }
  197. static void wcd937x_mbhc_mbhc_bias_control(struct snd_soc_codec *codec,
  198. bool enable)
  199. {
  200. if (enable)
  201. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_ELECT,
  202. 0x01, 0x01);
  203. else
  204. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_ELECT,
  205. 0x01, 0x00);
  206. }
  207. static void wcd937x_mbhc_program_btn_thr(struct snd_soc_codec *codec,
  208. s16 *btn_low, s16 *btn_high,
  209. int num_btn, bool is_micbias)
  210. {
  211. int i;
  212. int vth;
  213. if (num_btn > WCD_MBHC_DEF_BUTTONS) {
  214. dev_err(codec->dev, "%s: invalid number of buttons: %d\n",
  215. __func__, num_btn);
  216. return;
  217. }
  218. for (i = 0; i < num_btn; i++) {
  219. vth = ((btn_high[i] * 2) / 25) & 0x3F;
  220. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_BTN0 + i,
  221. 0xFC, vth << 2);
  222. dev_dbg(codec->dev, "%s: btn_high[%d]: %d, vth: %d\n",
  223. __func__, i, btn_high[i], vth);
  224. }
  225. }
  226. static bool wcd937x_mbhc_lock_sleep(struct wcd_mbhc *mbhc, bool lock)
  227. {
  228. return true;
  229. }
  230. static int wcd937x_mbhc_register_notifier(struct wcd_mbhc *mbhc,
  231. struct notifier_block *nblock,
  232. bool enable)
  233. {
  234. struct wcd937x_mbhc *wcd937x_mbhc;
  235. wcd937x_mbhc = container_of(mbhc, struct wcd937x_mbhc, wcd_mbhc);
  236. if (enable)
  237. return blocking_notifier_chain_register(&wcd937x_mbhc->notifier,
  238. nblock);
  239. else
  240. return blocking_notifier_chain_unregister(
  241. &wcd937x_mbhc->notifier, nblock);
  242. }
  243. static bool wcd937x_mbhc_micb_en_status(struct wcd_mbhc *mbhc, int micb_num)
  244. {
  245. u8 val = 0;
  246. if (micb_num == MIC_BIAS_2) {
  247. val = ((snd_soc_read(mbhc->codec, WCD937X_ANA_MICB2) & 0xC0)
  248. >> 6);
  249. if (val == 0x01)
  250. return true;
  251. }
  252. return false;
  253. }
  254. static bool wcd937x_mbhc_hph_pa_on_status(struct snd_soc_codec *codec)
  255. {
  256. return (snd_soc_read(codec, WCD937X_ANA_HPH) & 0xC0) ? true : false;
  257. }
  258. static void wcd937x_mbhc_hph_l_pull_up_control(struct snd_soc_codec *codec,
  259. int pull_up_cur)
  260. {
  261. /* Default pull up current to 2uA */
  262. if (pull_up_cur > HS_PULLUP_I_OFF || pull_up_cur < HS_PULLUP_I_3P0_UA ||
  263. pull_up_cur == HS_PULLUP_I_DEFAULT)
  264. pull_up_cur = HS_PULLUP_I_2P0_UA;
  265. dev_dbg(codec->dev, "%s: HS pull up current:%d\n",
  266. __func__, pull_up_cur);
  267. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_INT_MECH_DET_CURRENT,
  268. 0x1F, pull_up_cur);
  269. }
  270. static int wcd937x_mbhc_request_micbias(struct snd_soc_codec *codec,
  271. int micb_num, int req)
  272. {
  273. int ret = 0;
  274. ret = wcd937x_micbias_control(codec, micb_num, req, false);
  275. return ret;
  276. }
  277. static void wcd937x_mbhc_micb_ramp_control(struct snd_soc_codec *codec,
  278. bool enable)
  279. {
  280. if (enable) {
  281. snd_soc_update_bits(codec, WCD937X_ANA_MICB2_RAMP,
  282. 0x1C, 0x0C);
  283. snd_soc_update_bits(codec, WCD937X_ANA_MICB2_RAMP,
  284. 0x80, 0x80);
  285. } else {
  286. snd_soc_update_bits(codec, WCD937X_ANA_MICB2_RAMP,
  287. 0x80, 0x00);
  288. snd_soc_update_bits(codec, WCD937X_ANA_MICB2_RAMP,
  289. 0x1C, 0x00);
  290. }
  291. }
  292. static struct firmware_cal *wcd937x_get_hwdep_fw_cal(struct wcd_mbhc *mbhc,
  293. enum wcd_cal_type type)
  294. {
  295. struct wcd937x_mbhc *wcd937x_mbhc;
  296. struct firmware_cal *hwdep_cal;
  297. struct snd_soc_codec *codec = mbhc->codec;
  298. wcd937x_mbhc = container_of(mbhc, struct wcd937x_mbhc, wcd_mbhc);
  299. if (!codec) {
  300. pr_err("%s: NULL codec pointer\n", __func__);
  301. return NULL;
  302. }
  303. hwdep_cal = wcdcal_get_fw_cal(wcd937x_mbhc->fw_data, type);
  304. if (!hwdep_cal)
  305. dev_err(codec->dev, "%s: cal not sent by %d\n",
  306. __func__, type);
  307. return hwdep_cal;
  308. }
  309. static int wcd937x_mbhc_micb_ctrl_threshold_mic(struct snd_soc_codec *codec,
  310. int micb_num, bool req_en)
  311. {
  312. struct wcd937x_pdata *pdata = dev_get_platdata(codec->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 = wcd937x_mbhc_micb_adjust_voltage(codec, micb_mv, MIC_BIAS_2);
  325. return rc;
  326. }
  327. static inline void wcd937x_mbhc_get_result_params(struct wcd937x_priv *wcd937x,
  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(wcd937x->regmap, WCD937X_ANA_MBHC_ZDET, 0x20, 0x20);
  340. for (i = 0; i < WCD937X_ZDET_NUM_MEASUREMENTS; i++) {
  341. regmap_read(wcd937x->regmap, WCD937X_ANA_MBHC_RESULT_2, &val);
  342. if (val & 0x80)
  343. break;
  344. }
  345. val = val << 0x8;
  346. regmap_read(wcd937x->regmap, WCD937X_ANA_MBHC_RESULT_1, &val1);
  347. val |= val1;
  348. regmap_update_bits(wcd937x->regmap, WCD937X_ANA_MBHC_ZDET, 0x20, 0x00);
  349. x1 = WCD937X_MBHC_GET_X1(val);
  350. c1 = WCD937X_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(wcd937x->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 = (WCD937X_MBHC_ZDET_CONST * 1000) / denom;
  364. else if (x1 < minCode_param[noff])
  365. *zdet = WCD937X_ZDET_FLOATING_IMPEDANCE;
  366. dev_dbg(wcd937x->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_bulk_read(wcd937x->regmap,
  372. WCD937X_ANA_MBHC_RESULT_1, (u8 *)&val, 2);
  373. x1 = WCD937X_MBHC_GET_X1(val);
  374. i++;
  375. if (i == WCD937X_ZDET_NUM_MEASUREMENTS)
  376. break;
  377. }
  378. }
  379. static void wcd937x_mbhc_zdet_ramp(struct snd_soc_codec *codec,
  380. struct wcd937x_mbhc_zdet_param *zdet_param,
  381. int32_t *zl, int32_t *zr, s16 *d1_a)
  382. {
  383. struct wcd937x_priv *wcd937x = dev_get_drvdata(codec->dev);
  384. int32_t zdet = 0;
  385. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_ZDET_ANA_CTL, 0x70,
  386. zdet_param->ldo_ctl << 4);
  387. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_BTN5, 0xFC,
  388. zdet_param->btn5);
  389. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_BTN6, 0xFC,
  390. zdet_param->btn6);
  391. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_BTN7, 0xFC,
  392. zdet_param->btn7);
  393. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_ZDET_ANA_CTL, 0x0F,
  394. zdet_param->noff);
  395. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_ZDET_RAMP_CTL, 0x0F,
  396. zdet_param->nshift);
  397. if (!zl)
  398. goto z_right;
  399. /* Start impedance measurement for HPH_L */
  400. regmap_update_bits(wcd937x->regmap,
  401. WCD937X_ANA_MBHC_ZDET, 0x80, 0x80);
  402. dev_dbg(wcd937x->dev, "%s: ramp for HPH_L, noff = %d\n",
  403. __func__, zdet_param->noff);
  404. wcd937x_mbhc_get_result_params(wcd937x, d1_a, zdet_param->noff, &zdet);
  405. regmap_update_bits(wcd937x->regmap,
  406. WCD937X_ANA_MBHC_ZDET, 0x80, 0x00);
  407. *zl = zdet;
  408. z_right:
  409. if (!zr)
  410. return;
  411. /* Start impedance measurement for HPH_R */
  412. regmap_update_bits(wcd937x->regmap,
  413. WCD937X_ANA_MBHC_ZDET, 0x40, 0x40);
  414. dev_dbg(wcd937x->dev, "%s: ramp for HPH_R, noff = %d\n",
  415. __func__, zdet_param->noff);
  416. wcd937x_mbhc_get_result_params(wcd937x, d1_a, zdet_param->noff, &zdet);
  417. regmap_update_bits(wcd937x->regmap,
  418. WCD937X_ANA_MBHC_ZDET, 0x40, 0x00);
  419. *zr = zdet;
  420. }
  421. static inline void wcd937x_wcd_mbhc_qfuse_cal(struct snd_soc_codec *codec,
  422. int32_t *z_val, int flag_l_r)
  423. {
  424. s16 q1;
  425. int q1_cal;
  426. if (*z_val < (WCD937X_ZDET_VAL_400/1000))
  427. q1 = snd_soc_read(codec,
  428. WCD937X_DIGITAL_EFUSE_REG_23 + (2 * flag_l_r));
  429. else
  430. q1 = snd_soc_read(codec,
  431. WCD937X_DIGITAL_EFUSE_REG_24 + (2 * flag_l_r));
  432. if (q1 & 0x80)
  433. q1_cal = (10000 - ((q1 & 0x7F) * 25));
  434. else
  435. q1_cal = (10000 + (q1 * 25));
  436. if (q1_cal > 0)
  437. *z_val = ((*z_val) * 10000) / q1_cal;
  438. }
  439. static void wcd937x_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
  440. uint32_t *zr)
  441. {
  442. struct snd_soc_codec *codec = mbhc->codec;
  443. struct wcd937x_priv *wcd937x = dev_get_drvdata(codec->dev);
  444. s16 reg0, reg1, reg2, reg3, reg4;
  445. int32_t z1L, z1R, z1Ls;
  446. int zMono, z_diff1, z_diff2;
  447. bool is_fsm_disable = false;
  448. struct wcd937x_mbhc_zdet_param zdet_param[] = {
  449. {4, 0, 4, 0x08, 0x14, 0x18}, /* < 32ohm */
  450. {2, 0, 3, 0x18, 0x7C, 0x90}, /* 32ohm < Z < 400ohm */
  451. {1, 4, 5, 0x18, 0x7C, 0x90}, /* 400ohm < Z < 1200ohm */
  452. {1, 6, 7, 0x18, 0x7C, 0x90}, /* >1200ohm */
  453. };
  454. struct wcd937x_mbhc_zdet_param *zdet_param_ptr = NULL;
  455. s16 d1_a[][4] = {
  456. {0, 30, 90, 30},
  457. {0, 30, 30, 5},
  458. {0, 30, 30, 5},
  459. {0, 30, 30, 5},
  460. };
  461. s16 *d1 = NULL;
  462. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  463. reg0 = snd_soc_read(codec, WCD937X_ANA_MBHC_BTN5);
  464. reg1 = snd_soc_read(codec, WCD937X_ANA_MBHC_BTN6);
  465. reg2 = snd_soc_read(codec, WCD937X_ANA_MBHC_BTN7);
  466. reg3 = snd_soc_read(codec, WCD937X_MBHC_CTL_CLK);
  467. reg4 = snd_soc_read(codec, WCD937X_MBHC_NEW_ZDET_ANA_CTL);
  468. if (snd_soc_read(codec, WCD937X_ANA_MBHC_ELECT) & 0x80) {
  469. is_fsm_disable = true;
  470. regmap_update_bits(wcd937x->regmap,
  471. WCD937X_ANA_MBHC_ELECT, 0x80, 0x00);
  472. }
  473. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  474. if (mbhc->hphl_swh)
  475. regmap_update_bits(wcd937x->regmap,
  476. WCD937X_ANA_MBHC_MECH, 0x80, 0x00);
  477. /* Turn off 100k pull down on HPHL */
  478. regmap_update_bits(wcd937x->regmap,
  479. WCD937X_ANA_MBHC_MECH, 0x01, 0x00);
  480. /* First get impedance on Left */
  481. d1 = d1_a[1];
  482. zdet_param_ptr = &zdet_param[1];
  483. wcd937x_mbhc_zdet_ramp(codec, zdet_param_ptr, &z1L, NULL, d1);
  484. if (!WCD937X_MBHC_IS_SECOND_RAMP_REQUIRED(z1L))
  485. goto left_ch_impedance;
  486. /* Second ramp for left ch */
  487. if (z1L < WCD937X_ZDET_VAL_32) {
  488. zdet_param_ptr = &zdet_param[0];
  489. d1 = d1_a[0];
  490. } else if ((z1L > WCD937X_ZDET_VAL_400) &&
  491. (z1L <= WCD937X_ZDET_VAL_1200)) {
  492. zdet_param_ptr = &zdet_param[2];
  493. d1 = d1_a[2];
  494. } else if (z1L > WCD937X_ZDET_VAL_1200) {
  495. zdet_param_ptr = &zdet_param[3];
  496. d1 = d1_a[3];
  497. }
  498. wcd937x_mbhc_zdet_ramp(codec, zdet_param_ptr, &z1L, NULL, d1);
  499. left_ch_impedance:
  500. if ((z1L == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  501. (z1L > WCD937X_ZDET_VAL_100K)) {
  502. *zl = WCD937X_ZDET_FLOATING_IMPEDANCE;
  503. zdet_param_ptr = &zdet_param[1];
  504. d1 = d1_a[1];
  505. } else {
  506. *zl = z1L/1000;
  507. wcd937x_wcd_mbhc_qfuse_cal(codec, zl, 0);
  508. }
  509. dev_dbg(codec->dev, "%s: impedance on HPH_L = %d(ohms)\n",
  510. __func__, *zl);
  511. /* Start of right impedance ramp and calculation */
  512. wcd937x_mbhc_zdet_ramp(codec, zdet_param_ptr, NULL, &z1R, d1);
  513. if (WCD937X_MBHC_IS_SECOND_RAMP_REQUIRED(z1R)) {
  514. if (((z1R > WCD937X_ZDET_VAL_1200) &&
  515. (zdet_param_ptr->noff == 0x6)) ||
  516. ((*zl) != WCD937X_ZDET_FLOATING_IMPEDANCE))
  517. goto right_ch_impedance;
  518. /* Second ramp for right ch */
  519. if (z1R < WCD937X_ZDET_VAL_32) {
  520. zdet_param_ptr = &zdet_param[0];
  521. d1 = d1_a[0];
  522. } else if ((z1R > WCD937X_ZDET_VAL_400) &&
  523. (z1R <= WCD937X_ZDET_VAL_1200)) {
  524. zdet_param_ptr = &zdet_param[2];
  525. d1 = d1_a[2];
  526. } else if (z1R > WCD937X_ZDET_VAL_1200) {
  527. zdet_param_ptr = &zdet_param[3];
  528. d1 = d1_a[3];
  529. }
  530. wcd937x_mbhc_zdet_ramp(codec, zdet_param_ptr, NULL, &z1R, d1);
  531. }
  532. right_ch_impedance:
  533. if ((z1R == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  534. (z1R > WCD937X_ZDET_VAL_100K)) {
  535. *zr = WCD937X_ZDET_FLOATING_IMPEDANCE;
  536. } else {
  537. *zr = z1R/1000;
  538. wcd937x_wcd_mbhc_qfuse_cal(codec, zr, 1);
  539. }
  540. dev_dbg(codec->dev, "%s: impedance on HPH_R = %d(ohms)\n",
  541. __func__, *zr);
  542. /* Mono/stereo detection */
  543. if ((*zl == WCD937X_ZDET_FLOATING_IMPEDANCE) &&
  544. (*zr == WCD937X_ZDET_FLOATING_IMPEDANCE)) {
  545. dev_dbg(codec->dev,
  546. "%s: plug type is invalid or extension cable\n",
  547. __func__);
  548. goto zdet_complete;
  549. }
  550. if ((*zl == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  551. (*zr == WCD937X_ZDET_FLOATING_IMPEDANCE) ||
  552. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  553. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  554. dev_dbg(codec->dev,
  555. "%s: Mono plug type with one ch floating or shorted to GND\n",
  556. __func__);
  557. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  558. goto zdet_complete;
  559. }
  560. snd_soc_update_bits(codec, WCD937X_HPH_R_ATEST, 0x02, 0x02);
  561. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2, 0x40, 0x01);
  562. if (*zl < (WCD937X_ZDET_VAL_32/1000))
  563. wcd937x_mbhc_zdet_ramp(codec, &zdet_param[0], &z1Ls, NULL, d1);
  564. else
  565. wcd937x_mbhc_zdet_ramp(codec, &zdet_param[1], &z1Ls, NULL, d1);
  566. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2, 0x40, 0x00);
  567. snd_soc_update_bits(codec, WCD937X_HPH_R_ATEST, 0x02, 0x00);
  568. z1Ls /= 1000;
  569. wcd937x_wcd_mbhc_qfuse_cal(codec, &z1Ls, 0);
  570. /* Parallel of left Z and 9 ohm pull down resistor */
  571. zMono = ((*zl) * 9) / ((*zl) + 9);
  572. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  573. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  574. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  575. dev_dbg(codec->dev, "%s: stereo plug type detected\n",
  576. __func__);
  577. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  578. } else {
  579. dev_dbg(codec->dev, "%s: MONO plug type detected\n",
  580. __func__);
  581. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  582. }
  583. zdet_complete:
  584. snd_soc_write(codec, WCD937X_ANA_MBHC_BTN5, reg0);
  585. snd_soc_write(codec, WCD937X_ANA_MBHC_BTN6, reg1);
  586. snd_soc_write(codec, WCD937X_ANA_MBHC_BTN7, reg2);
  587. /* Turn on 100k pull down on HPHL */
  588. regmap_update_bits(wcd937x->regmap,
  589. WCD937X_ANA_MBHC_MECH, 0x01, 0x01);
  590. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  591. if (mbhc->hphl_swh)
  592. regmap_update_bits(wcd937x->regmap,
  593. WCD937X_ANA_MBHC_MECH, 0x80, 0x80);
  594. snd_soc_write(codec, WCD937X_MBHC_NEW_ZDET_ANA_CTL, reg4);
  595. snd_soc_write(codec, WCD937X_MBHC_CTL_CLK, reg3);
  596. if (is_fsm_disable)
  597. regmap_update_bits(wcd937x->regmap,
  598. WCD937X_ANA_MBHC_ELECT, 0x80, 0x80);
  599. }
  600. static void wcd937x_mbhc_gnd_det_ctrl(struct snd_soc_codec *codec, bool enable)
  601. {
  602. if (enable) {
  603. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_MECH,
  604. 0x02, 0x02);
  605. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_MECH,
  606. 0x40, 0x40);
  607. } else {
  608. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_MECH,
  609. 0x40, 0x00);
  610. snd_soc_update_bits(codec, WCD937X_ANA_MBHC_MECH,
  611. 0x02, 0x00);
  612. }
  613. }
  614. static void wcd937x_mbhc_hph_pull_down_ctrl(struct snd_soc_codec *codec,
  615. bool enable)
  616. {
  617. if (enable) {
  618. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2,
  619. 0x40, 0x40);
  620. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2,
  621. 0x10, 0x10);
  622. } else {
  623. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2,
  624. 0x40, 0x00);
  625. snd_soc_update_bits(codec, WCD937X_HPH_PA_CTL2,
  626. 0x10, 0x00);
  627. }
  628. }
  629. static void wcd937x_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  630. {
  631. struct snd_soc_codec *codec = mbhc->codec;
  632. if ((mbhc->moist_rref == R_OFF) ||
  633. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  634. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  635. 0x0C, R_OFF << 2);
  636. return;
  637. }
  638. /* Do not enable moisture detection if jack type is NC */
  639. if (!mbhc->hphl_swh) {
  640. dev_dbg(codec->dev, "%s: disable moisture detection for NC\n",
  641. __func__);
  642. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  643. 0x0C, R_OFF << 2);
  644. return;
  645. }
  646. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  647. 0x0C, mbhc->moist_rref << 2);
  648. }
  649. static void wcd937x_mbhc_moisture_detect_en(struct wcd_mbhc *mbhc, bool enable)
  650. {
  651. struct snd_soc_codec *codec = mbhc->codec;
  652. if (enable)
  653. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  654. 0x0C, mbhc->moist_rref << 2);
  655. else
  656. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  657. 0x0C, R_OFF << 2);
  658. }
  659. static bool wcd937x_mbhc_get_moisture_status(struct wcd_mbhc *mbhc)
  660. {
  661. struct snd_soc_codec *codec = mbhc->codec;
  662. bool ret = false;
  663. if ((mbhc->moist_rref == R_OFF) ||
  664. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  665. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  666. 0x0C, R_OFF << 2);
  667. goto done;
  668. }
  669. /* Do not enable moisture detection if jack type is NC */
  670. if (!mbhc->hphl_swh) {
  671. dev_dbg(codec->dev, "%s: disable moisture detection for NC\n",
  672. __func__);
  673. snd_soc_update_bits(codec, WCD937X_MBHC_NEW_CTL_2,
  674. 0x0C, R_OFF << 2);
  675. goto done;
  676. }
  677. /* If moisture_en is already enabled, then skip to plug type
  678. * detection.
  679. */
  680. if ((snd_soc_read(codec, WCD937X_MBHC_NEW_CTL_2) & 0x0C))
  681. goto done;
  682. wcd937x_mbhc_moisture_detect_en(mbhc, true);
  683. /* Read moisture comparator status */
  684. ret = ((snd_soc_read(codec, WCD937X_MBHC_NEW_FSM_STATUS)
  685. & 0x20) ? 0 : 1);
  686. done:
  687. return ret;
  688. }
  689. static void wcd937x_mbhc_moisture_polling_ctrl(struct wcd_mbhc *mbhc,
  690. bool enable)
  691. {
  692. struct snd_soc_codec *codec = mbhc->codec;
  693. snd_soc_update_bits(codec,
  694. WCD937X_MBHC_NEW_INT_MOISTURE_DET_POLLING_CTRL,
  695. 0x04, (enable << 2));
  696. }
  697. static const struct wcd_mbhc_cb mbhc_cb = {
  698. .request_irq = wcd937x_mbhc_request_irq,
  699. .irq_control = wcd937x_mbhc_irq_control,
  700. .free_irq = wcd937x_mbhc_free_irq,
  701. .clk_setup = wcd937x_mbhc_clk_setup,
  702. .map_btn_code_to_num = wcd937x_mbhc_btn_to_num,
  703. .mbhc_bias = wcd937x_mbhc_mbhc_bias_control,
  704. .set_btn_thr = wcd937x_mbhc_program_btn_thr,
  705. .lock_sleep = wcd937x_mbhc_lock_sleep,
  706. .register_notifier = wcd937x_mbhc_register_notifier,
  707. .micbias_enable_status = wcd937x_mbhc_micb_en_status,
  708. .hph_pa_on_status = wcd937x_mbhc_hph_pa_on_status,
  709. .hph_pull_up_control_v2 = wcd937x_mbhc_hph_l_pull_up_control,
  710. .mbhc_micbias_control = wcd937x_mbhc_request_micbias,
  711. .mbhc_micb_ramp_control = wcd937x_mbhc_micb_ramp_control,
  712. .get_hwdep_fw_cal = wcd937x_get_hwdep_fw_cal,
  713. .mbhc_micb_ctrl_thr_mic = wcd937x_mbhc_micb_ctrl_threshold_mic,
  714. .compute_impedance = wcd937x_wcd_mbhc_calc_impedance,
  715. .mbhc_gnd_det_ctrl = wcd937x_mbhc_gnd_det_ctrl,
  716. .hph_pull_down_ctrl = wcd937x_mbhc_hph_pull_down_ctrl,
  717. .mbhc_moisture_config = wcd937x_mbhc_moisture_config,
  718. .mbhc_get_moisture_status = wcd937x_mbhc_get_moisture_status,
  719. .mbhc_moisture_polling_ctrl = wcd937x_mbhc_moisture_polling_ctrl,
  720. .mbhc_moisture_detect_en = wcd937x_mbhc_moisture_detect_en,
  721. };
  722. static int wcd937x_get_hph_type(struct snd_kcontrol *kcontrol,
  723. struct snd_ctl_elem_value *ucontrol)
  724. {
  725. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  726. struct wcd937x_mbhc *wcd937x_mbhc = wcd937x_soc_get_mbhc(codec);
  727. struct wcd_mbhc *mbhc;
  728. if (!wcd937x_mbhc) {
  729. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  730. return -EINVAL;
  731. }
  732. mbhc = &wcd937x_mbhc->wcd_mbhc;
  733. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  734. dev_dbg(codec->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  735. return 0;
  736. }
  737. static int wcd937x_hph_impedance_get(struct snd_kcontrol *kcontrol,
  738. struct snd_ctl_elem_value *ucontrol)
  739. {
  740. uint32_t zl, zr;
  741. bool hphr;
  742. struct soc_multi_mixer_control *mc;
  743. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  744. struct wcd937x_mbhc *wcd937x_mbhc = wcd937x_soc_get_mbhc(codec);
  745. if (!wcd937x_mbhc) {
  746. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  747. return -EINVAL;
  748. }
  749. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  750. hphr = mc->shift;
  751. wcd_mbhc_get_impedance(&wcd937x_mbhc->wcd_mbhc, &zl, &zr);
  752. dev_dbg(codec->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  753. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  754. return 0;
  755. }
  756. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  757. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  758. wcd937x_get_hph_type, NULL),
  759. };
  760. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  761. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  762. wcd937x_hph_impedance_get, NULL),
  763. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  764. wcd937x_hph_impedance_get, NULL),
  765. };
  766. /*
  767. * wcd937x_mbhc_get_impedance: get impedance of headphone
  768. * left and right channels
  769. * @wcd937x_mbhc: handle to struct wcd937x_mbhc *
  770. * @zl: handle to left-ch impedance
  771. * @zr: handle to right-ch impedance
  772. * return 0 for success or error code in case of failure
  773. */
  774. int wcd937x_mbhc_get_impedance(struct wcd937x_mbhc *wcd937x_mbhc,
  775. uint32_t *zl, uint32_t *zr)
  776. {
  777. if (!wcd937x_mbhc) {
  778. pr_err("%s: mbhc not initialized!\n", __func__);
  779. return -EINVAL;
  780. }
  781. if (!zl || !zr) {
  782. pr_err("%s: zl or zr null!\n", __func__);
  783. return -EINVAL;
  784. }
  785. return wcd_mbhc_get_impedance(&wcd937x_mbhc->wcd_mbhc, zl, zr);
  786. }
  787. EXPORT_SYMBOL(wcd937x_mbhc_get_impedance);
  788. /*
  789. * wcd937x_mbhc_hs_detect: starts mbhc insertion/removal functionality
  790. * @codec: handle to snd_soc_codec *
  791. * @mbhc_cfg: handle to mbhc configuration structure
  792. * return 0 if mbhc_start is success or error code in case of failure
  793. */
  794. int wcd937x_mbhc_hs_detect(struct snd_soc_codec *codec,
  795. struct wcd_mbhc_config *mbhc_cfg)
  796. {
  797. struct wcd937x_priv *wcd937x = NULL;
  798. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  799. if (!codec) {
  800. pr_err("%s: codec is NULL\n", __func__);
  801. return -EINVAL;
  802. }
  803. wcd937x = snd_soc_codec_get_drvdata(codec);
  804. if (!wcd937x) {
  805. pr_err("%s: wcd937x is NULL\n", __func__);
  806. return -EINVAL;
  807. }
  808. wcd937x_mbhc = wcd937x->mbhc;
  809. if (!wcd937x_mbhc) {
  810. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  811. return -EINVAL;
  812. }
  813. return wcd_mbhc_start(&wcd937x_mbhc->wcd_mbhc, mbhc_cfg);
  814. }
  815. EXPORT_SYMBOL(wcd937x_mbhc_hs_detect);
  816. /*
  817. * wcd937x_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  818. * @codec: handle to snd_soc_codec *
  819. */
  820. void wcd937x_mbhc_hs_detect_exit(struct snd_soc_codec *codec)
  821. {
  822. struct wcd937x_priv *wcd937x = NULL;
  823. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  824. if (!codec) {
  825. pr_err("%s: codec is NULL\n", __func__);
  826. return;
  827. }
  828. wcd937x = snd_soc_codec_get_drvdata(codec);
  829. if (!wcd937x) {
  830. pr_err("%s: wcd937x is NULL\n", __func__);
  831. return;
  832. }
  833. wcd937x_mbhc = wcd937x->mbhc;
  834. if (!wcd937x_mbhc) {
  835. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  836. return;
  837. }
  838. wcd_mbhc_stop(&wcd937x_mbhc->wcd_mbhc);
  839. }
  840. EXPORT_SYMBOL(wcd937x_mbhc_hs_detect_exit);
  841. /*
  842. * wcd937x_mbhc_post_ssr_init: initialize mbhc for
  843. * wcd937x post subsystem restart
  844. * @mbhc: poniter to wcd937x_mbhc structure
  845. * @codec: handle to snd_soc_codec *
  846. *
  847. * return 0 if mbhc_init is success or error code in case of failure
  848. */
  849. int wcd937x_mbhc_post_ssr_init(struct wcd937x_mbhc *mbhc,
  850. struct snd_soc_codec *codec)
  851. {
  852. int ret = 0;
  853. struct wcd_mbhc *wcd_mbhc = NULL;
  854. if (!mbhc || !codec)
  855. return -EINVAL;
  856. wcd_mbhc = &mbhc->wcd_mbhc;
  857. if (wcd_mbhc == NULL) {
  858. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  859. return -EINVAL;
  860. }
  861. wcd_mbhc_deinit(wcd_mbhc);
  862. ret = wcd_mbhc_init(wcd_mbhc, codec, &mbhc_cb, &intr_ids,
  863. wcd_mbhc_registers, WCD937X_ZDET_SUPPORTED);
  864. if (ret) {
  865. dev_err(codec->dev, "%s: mbhc initialization failed\n",
  866. __func__);
  867. goto done;
  868. }
  869. done:
  870. return ret;
  871. }
  872. EXPORT_SYMBOL(wcd937x_mbhc_post_ssr_init);
  873. /*
  874. * wcd937x_mbhc_init: initialize mbhc for wcd937x
  875. * @mbhc: poniter to wcd937x_mbhc struct pointer to store the configs
  876. * @codec: handle to snd_soc_codec *
  877. * @fw_data: handle to firmware data
  878. *
  879. * return 0 if mbhc_init is success or error code in case of failure
  880. */
  881. int wcd937x_mbhc_init(struct wcd937x_mbhc **mbhc, struct snd_soc_codec *codec,
  882. struct fw_info *fw_data)
  883. {
  884. struct wcd937x_mbhc *wcd937x_mbhc = NULL;
  885. struct wcd_mbhc *wcd_mbhc = NULL;
  886. int ret = 0;
  887. if (!codec) {
  888. pr_err("%s: codec is NULL\n", __func__);
  889. return -EINVAL;
  890. }
  891. wcd937x_mbhc = devm_kzalloc(codec->dev, sizeof(struct wcd937x_mbhc),
  892. GFP_KERNEL);
  893. if (!wcd937x_mbhc)
  894. return -ENOMEM;
  895. wcd937x_mbhc->fw_data = fw_data;
  896. BLOCKING_INIT_NOTIFIER_HEAD(&wcd937x_mbhc->notifier);
  897. wcd_mbhc = &wcd937x_mbhc->wcd_mbhc;
  898. if (wcd_mbhc == NULL) {
  899. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  900. ret = -EINVAL;
  901. goto err;
  902. }
  903. /* Setting default mbhc detection logic to ADC */
  904. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  905. ret = wcd_mbhc_init(wcd_mbhc, codec, &mbhc_cb,
  906. &intr_ids, wcd_mbhc_registers,
  907. WCD937X_ZDET_SUPPORTED);
  908. if (ret) {
  909. dev_err(codec->dev, "%s: mbhc initialization failed\n",
  910. __func__);
  911. goto err;
  912. }
  913. (*mbhc) = wcd937x_mbhc;
  914. snd_soc_add_codec_controls(codec, impedance_detect_controls,
  915. ARRAY_SIZE(impedance_detect_controls));
  916. snd_soc_add_codec_controls(codec, hph_type_detect_controls,
  917. ARRAY_SIZE(hph_type_detect_controls));
  918. return 0;
  919. err:
  920. devm_kfree(codec->dev, wcd937x_mbhc);
  921. return ret;
  922. }
  923. EXPORT_SYMBOL(wcd937x_mbhc_init);
  924. /*
  925. * wcd937x_mbhc_deinit: deinitialize mbhc for wcd937x
  926. * @codec: handle to snd_soc_codec *
  927. */
  928. void wcd937x_mbhc_deinit(struct snd_soc_codec *codec)
  929. {
  930. struct wcd937x_priv *wcd937x;
  931. struct wcd937x_mbhc *wcd937x_mbhc;
  932. if (!codec) {
  933. pr_err("%s: codec is NULL\n", __func__);
  934. return;
  935. }
  936. wcd937x = snd_soc_codec_get_drvdata(codec);
  937. if (!wcd937x) {
  938. pr_err("%s: wcd937x is NULL\n", __func__);
  939. return;
  940. }
  941. wcd937x_mbhc = wcd937x->mbhc;
  942. if (wcd937x_mbhc) {
  943. wcd_mbhc_deinit(&wcd937x_mbhc->wcd_mbhc);
  944. devm_kfree(codec->dev, wcd937x_mbhc);
  945. }
  946. }
  947. EXPORT_SYMBOL(wcd937x_mbhc_deinit);