wcd938x-mbhc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. /* Disable surge protection before impedance detection.
  489. * This is done to give correct value for high impedance.
  490. */
  491. regmap_update_bits(wcd938x->regmap,
  492. WCD938X_HPH_SURGE_HPHLR_SURGE_EN, 0xC0, 0x00);
  493. /* 1ms delay needed after disable surge protection */
  494. usleep_range(1000, 1010);
  495. /* First get impedance on Left */
  496. d1 = d1_a[1];
  497. zdet_param_ptr = &zdet_param[1];
  498. wcd938x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  499. if (!WCD938X_MBHC_IS_SECOND_RAMP_REQUIRED(z1L))
  500. goto left_ch_impedance;
  501. /* Second ramp for left ch */
  502. if (z1L < WCD938X_ZDET_VAL_32) {
  503. zdet_param_ptr = &zdet_param[0];
  504. d1 = d1_a[0];
  505. } else if ((z1L > WCD938X_ZDET_VAL_400) &&
  506. (z1L <= WCD938X_ZDET_VAL_1200)) {
  507. zdet_param_ptr = &zdet_param[2];
  508. d1 = d1_a[2];
  509. } else if (z1L > WCD938X_ZDET_VAL_1200) {
  510. zdet_param_ptr = &zdet_param[3];
  511. d1 = d1_a[3];
  512. }
  513. wcd938x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  514. left_ch_impedance:
  515. if ((z1L == WCD938X_ZDET_FLOATING_IMPEDANCE) ||
  516. (z1L > WCD938X_ZDET_VAL_100K)) {
  517. *zl = WCD938X_ZDET_FLOATING_IMPEDANCE;
  518. zdet_param_ptr = &zdet_param[1];
  519. d1 = d1_a[1];
  520. } else {
  521. *zl = z1L/1000;
  522. wcd938x_wcd_mbhc_qfuse_cal(component, zl, 0);
  523. }
  524. dev_dbg(component->dev, "%s: impedance on HPH_L = %d(ohms)\n",
  525. __func__, *zl);
  526. /* Start of right impedance ramp and calculation */
  527. wcd938x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
  528. if (WCD938X_MBHC_IS_SECOND_RAMP_REQUIRED(z1R)) {
  529. if (((z1R > WCD938X_ZDET_VAL_1200) &&
  530. (zdet_param_ptr->noff == 0x6)) ||
  531. ((*zl) != WCD938X_ZDET_FLOATING_IMPEDANCE))
  532. goto right_ch_impedance;
  533. /* Second ramp for right ch */
  534. if (z1R < WCD938X_ZDET_VAL_32) {
  535. zdet_param_ptr = &zdet_param[0];
  536. d1 = d1_a[0];
  537. } else if ((z1R > WCD938X_ZDET_VAL_400) &&
  538. (z1R <= WCD938X_ZDET_VAL_1200)) {
  539. zdet_param_ptr = &zdet_param[2];
  540. d1 = d1_a[2];
  541. } else if (z1R > WCD938X_ZDET_VAL_1200) {
  542. zdet_param_ptr = &zdet_param[3];
  543. d1 = d1_a[3];
  544. }
  545. wcd938x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
  546. }
  547. right_ch_impedance:
  548. if ((z1R == WCD938X_ZDET_FLOATING_IMPEDANCE) ||
  549. (z1R > WCD938X_ZDET_VAL_100K)) {
  550. *zr = WCD938X_ZDET_FLOATING_IMPEDANCE;
  551. } else {
  552. *zr = z1R/1000;
  553. wcd938x_wcd_mbhc_qfuse_cal(component, zr, 1);
  554. }
  555. dev_dbg(component->dev, "%s: impedance on HPH_R = %d(ohms)\n",
  556. __func__, *zr);
  557. /* Mono/stereo detection */
  558. if ((*zl == WCD938X_ZDET_FLOATING_IMPEDANCE) &&
  559. (*zr == WCD938X_ZDET_FLOATING_IMPEDANCE)) {
  560. dev_dbg(component->dev,
  561. "%s: plug type is invalid or extension cable\n",
  562. __func__);
  563. goto zdet_complete;
  564. }
  565. if ((*zl == WCD938X_ZDET_FLOATING_IMPEDANCE) ||
  566. (*zr == WCD938X_ZDET_FLOATING_IMPEDANCE) ||
  567. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  568. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  569. dev_dbg(component->dev,
  570. "%s: Mono plug type with one ch floating or shorted to GND\n",
  571. __func__);
  572. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  573. goto zdet_complete;
  574. }
  575. snd_soc_component_update_bits(component, WCD938X_HPH_R_ATEST, 0x02, 0x02);
  576. snd_soc_component_update_bits(component, WCD938X_HPH_PA_CTL2, 0x40, 0x01);
  577. if (*zl < (WCD938X_ZDET_VAL_32/1000))
  578. wcd938x_mbhc_zdet_ramp(component, &zdet_param[0], &z1Ls, NULL, d1);
  579. else
  580. wcd938x_mbhc_zdet_ramp(component, &zdet_param[1], &z1Ls, NULL, d1);
  581. snd_soc_component_update_bits(component, WCD938X_HPH_PA_CTL2, 0x40, 0x00);
  582. snd_soc_component_update_bits(component, WCD938X_HPH_R_ATEST, 0x02, 0x00);
  583. z1Ls /= 1000;
  584. wcd938x_wcd_mbhc_qfuse_cal(component, &z1Ls, 0);
  585. /* Parallel of left Z and 9 ohm pull down resistor */
  586. zMono = ((*zl) * 9) / ((*zl) + 9);
  587. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  588. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  589. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  590. dev_dbg(component->dev, "%s: stereo plug type detected\n",
  591. __func__);
  592. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  593. } else {
  594. dev_dbg(component->dev, "%s: MONO plug type detected\n",
  595. __func__);
  596. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  597. }
  598. /* Enable surge protection again after impedance detection */
  599. regmap_update_bits(wcd938x->regmap,
  600. WCD938X_HPH_SURGE_HPHLR_SURGE_EN, 0xC0, 0xC0);
  601. zdet_complete:
  602. snd_soc_component_write(component, WCD938X_ANA_MBHC_BTN5, reg0);
  603. snd_soc_component_write(component, WCD938X_ANA_MBHC_BTN6, reg1);
  604. snd_soc_component_write(component, WCD938X_ANA_MBHC_BTN7, reg2);
  605. /* Turn on 100k pull down on HPHL */
  606. regmap_update_bits(wcd938x->regmap,
  607. WCD938X_ANA_MBHC_MECH, 0x01, 0x01);
  608. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  609. if (mbhc->hphl_swh)
  610. regmap_update_bits(wcd938x->regmap,
  611. WCD938X_ANA_MBHC_MECH, 0x80, 0x80);
  612. snd_soc_component_write(component, WCD938X_MBHC_NEW_ZDET_ANA_CTL, reg4);
  613. snd_soc_component_write(component, WCD938X_MBHC_CTL_CLK, reg3);
  614. if (is_fsm_disable)
  615. regmap_update_bits(wcd938x->regmap,
  616. WCD938X_ANA_MBHC_ELECT, 0x80, 0x80);
  617. }
  618. static void wcd938x_mbhc_gnd_det_ctrl(struct snd_soc_component *component,
  619. bool enable)
  620. {
  621. if (enable) {
  622. snd_soc_component_update_bits(component, WCD938X_ANA_MBHC_MECH,
  623. 0x02, 0x02);
  624. snd_soc_component_update_bits(component, WCD938X_ANA_MBHC_MECH,
  625. 0x40, 0x40);
  626. } else {
  627. snd_soc_component_update_bits(component, WCD938X_ANA_MBHC_MECH,
  628. 0x40, 0x00);
  629. snd_soc_component_update_bits(component, WCD938X_ANA_MBHC_MECH,
  630. 0x02, 0x00);
  631. }
  632. }
  633. static void wcd938x_mbhc_hph_pull_down_ctrl(struct snd_soc_component *component,
  634. bool enable)
  635. {
  636. if (enable) {
  637. snd_soc_component_update_bits(component, WCD938X_HPH_PA_CTL2,
  638. 0x40, 0x40);
  639. snd_soc_component_update_bits(component, WCD938X_HPH_PA_CTL2,
  640. 0x10, 0x10);
  641. } else {
  642. snd_soc_component_update_bits(component, WCD938X_HPH_PA_CTL2,
  643. 0x40, 0x00);
  644. snd_soc_component_update_bits(component, WCD938X_HPH_PA_CTL2,
  645. 0x10, 0x00);
  646. }
  647. }
  648. static void wcd938x_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  649. {
  650. struct snd_soc_component *component = mbhc->component;
  651. if ((mbhc->moist_rref == R_OFF) ||
  652. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  653. snd_soc_component_update_bits(component, WCD938X_MBHC_NEW_CTL_2,
  654. 0x0C, R_OFF << 2);
  655. return;
  656. }
  657. /* Do not enable moisture detection if jack type is NC */
  658. if (!mbhc->hphl_swh) {
  659. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  660. __func__);
  661. snd_soc_component_update_bits(component, WCD938X_MBHC_NEW_CTL_2,
  662. 0x0C, R_OFF << 2);
  663. return;
  664. }
  665. snd_soc_component_update_bits(component, WCD938X_MBHC_NEW_CTL_2,
  666. 0x0C, mbhc->moist_rref << 2);
  667. }
  668. static void wcd938x_mbhc_moisture_detect_en(struct wcd_mbhc *mbhc, bool enable)
  669. {
  670. struct snd_soc_component *component = mbhc->component;
  671. if (enable)
  672. snd_soc_component_update_bits(component, WCD938X_MBHC_NEW_CTL_2,
  673. 0x0C, mbhc->moist_rref << 2);
  674. else
  675. snd_soc_component_update_bits(component, WCD938X_MBHC_NEW_CTL_2,
  676. 0x0C, R_OFF << 2);
  677. }
  678. static bool wcd938x_mbhc_get_moisture_status(struct wcd_mbhc *mbhc)
  679. {
  680. struct snd_soc_component *component = mbhc->component;
  681. bool ret = false;
  682. if ((mbhc->moist_rref == R_OFF) ||
  683. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  684. snd_soc_component_update_bits(component, WCD938X_MBHC_NEW_CTL_2,
  685. 0x0C, R_OFF << 2);
  686. goto done;
  687. }
  688. /* Do not enable moisture detection if jack type is NC */
  689. if (!mbhc->hphl_swh) {
  690. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  691. __func__);
  692. snd_soc_component_update_bits(component, WCD938X_MBHC_NEW_CTL_2,
  693. 0x0C, R_OFF << 2);
  694. goto done;
  695. }
  696. /*
  697. * If moisture_en is already enabled, then skip to plug type
  698. * detection.
  699. */
  700. if ((snd_soc_component_read32(component, WCD938X_MBHC_NEW_CTL_2) & 0x0C))
  701. goto done;
  702. wcd938x_mbhc_moisture_detect_en(mbhc, true);
  703. /* Read moisture comparator status */
  704. ret = ((snd_soc_component_read32(component, WCD938X_MBHC_NEW_FSM_STATUS)
  705. & 0x20) ? 0 : 1);
  706. done:
  707. return ret;
  708. }
  709. static void wcd938x_mbhc_moisture_polling_ctrl(struct wcd_mbhc *mbhc,
  710. bool enable)
  711. {
  712. struct snd_soc_component *component = mbhc->component;
  713. snd_soc_component_update_bits(component,
  714. WCD938X_MBHC_NEW_INT_MOISTURE_DET_POLLING_CTRL,
  715. 0x04, (enable << 2));
  716. }
  717. static void wcd938x_mbhc_bcs_enable(struct wcd_mbhc *mbhc,
  718. bool bcs_enable)
  719. {
  720. if (bcs_enable)
  721. wcd938x_disable_bcs_before_slow_insert(mbhc->component, false);
  722. else
  723. wcd938x_disable_bcs_before_slow_insert(mbhc->component, true);
  724. }
  725. static const struct wcd_mbhc_cb mbhc_cb = {
  726. .request_irq = wcd938x_mbhc_request_irq,
  727. .irq_control = wcd938x_mbhc_irq_control,
  728. .free_irq = wcd938x_mbhc_free_irq,
  729. .clk_setup = wcd938x_mbhc_clk_setup,
  730. .map_btn_code_to_num = wcd938x_mbhc_btn_to_num,
  731. .mbhc_bias = wcd938x_mbhc_mbhc_bias_control,
  732. .set_btn_thr = wcd938x_mbhc_program_btn_thr,
  733. .lock_sleep = wcd938x_mbhc_lock_sleep,
  734. .register_notifier = wcd938x_mbhc_register_notifier,
  735. .micbias_enable_status = wcd938x_mbhc_micb_en_status,
  736. .hph_pa_on_status = wcd938x_mbhc_hph_pa_on_status,
  737. .hph_pull_up_control_v2 = wcd938x_mbhc_hph_l_pull_up_control,
  738. .mbhc_micbias_control = wcd938x_mbhc_request_micbias,
  739. .mbhc_micb_ramp_control = wcd938x_mbhc_micb_ramp_control,
  740. .get_hwdep_fw_cal = wcd938x_get_hwdep_fw_cal,
  741. .mbhc_micb_ctrl_thr_mic = wcd938x_mbhc_micb_ctrl_threshold_mic,
  742. .compute_impedance = wcd938x_wcd_mbhc_calc_impedance,
  743. .mbhc_gnd_det_ctrl = wcd938x_mbhc_gnd_det_ctrl,
  744. .hph_pull_down_ctrl = wcd938x_mbhc_hph_pull_down_ctrl,
  745. .mbhc_moisture_config = wcd938x_mbhc_moisture_config,
  746. .mbhc_get_moisture_status = wcd938x_mbhc_get_moisture_status,
  747. .mbhc_moisture_polling_ctrl = wcd938x_mbhc_moisture_polling_ctrl,
  748. .mbhc_moisture_detect_en = wcd938x_mbhc_moisture_detect_en,
  749. .bcs_enable = wcd938x_mbhc_bcs_enable,
  750. };
  751. static int wcd938x_get_hph_type(struct snd_kcontrol *kcontrol,
  752. struct snd_ctl_elem_value *ucontrol)
  753. {
  754. struct snd_soc_component *component =
  755. snd_soc_kcontrol_component(kcontrol);
  756. struct wcd938x_mbhc *wcd938x_mbhc = wcd938x_soc_get_mbhc(component);
  757. struct wcd_mbhc *mbhc;
  758. if (!wcd938x_mbhc) {
  759. dev_err(component->dev, "%s: mbhc not initialized!\n", __func__);
  760. return -EINVAL;
  761. }
  762. mbhc = &wcd938x_mbhc->wcd_mbhc;
  763. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  764. dev_dbg(component->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  765. return 0;
  766. }
  767. static int wcd938x_hph_impedance_get(struct snd_kcontrol *kcontrol,
  768. struct snd_ctl_elem_value *ucontrol)
  769. {
  770. uint32_t zl, zr;
  771. bool hphr;
  772. struct soc_multi_mixer_control *mc;
  773. struct snd_soc_component *component =
  774. snd_soc_kcontrol_component(kcontrol);
  775. struct wcd938x_mbhc *wcd938x_mbhc = wcd938x_soc_get_mbhc(component);
  776. if (!wcd938x_mbhc) {
  777. dev_err(component->dev, "%s: mbhc not initialized!\n", __func__);
  778. return -EINVAL;
  779. }
  780. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  781. hphr = mc->shift;
  782. wcd_mbhc_get_impedance(&wcd938x_mbhc->wcd_mbhc, &zl, &zr);
  783. dev_dbg(component->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  784. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  785. return 0;
  786. }
  787. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  788. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  789. wcd938x_get_hph_type, NULL),
  790. };
  791. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  792. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  793. wcd938x_hph_impedance_get, NULL),
  794. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  795. wcd938x_hph_impedance_get, NULL),
  796. };
  797. /*
  798. * wcd938x_mbhc_get_impedance: get impedance of headphone
  799. * left and right channels
  800. * @wcd938x_mbhc: handle to struct wcd938x_mbhc *
  801. * @zl: handle to left-ch impedance
  802. * @zr: handle to right-ch impedance
  803. * return 0 for success or error code in case of failure
  804. */
  805. int wcd938x_mbhc_get_impedance(struct wcd938x_mbhc *wcd938x_mbhc,
  806. uint32_t *zl, uint32_t *zr)
  807. {
  808. if (!wcd938x_mbhc) {
  809. pr_err("%s: mbhc not initialized!\n", __func__);
  810. return -EINVAL;
  811. }
  812. if (!zl || !zr) {
  813. pr_err("%s: zl or zr null!\n", __func__);
  814. return -EINVAL;
  815. }
  816. return wcd_mbhc_get_impedance(&wcd938x_mbhc->wcd_mbhc, zl, zr);
  817. }
  818. EXPORT_SYMBOL(wcd938x_mbhc_get_impedance);
  819. /*
  820. * wcd938x_mbhc_hs_detect: starts mbhc insertion/removal functionality
  821. * @codec: handle to snd_soc_component *
  822. * @mbhc_cfg: handle to mbhc configuration structure
  823. * return 0 if mbhc_start is success or error code in case of failure
  824. */
  825. int wcd938x_mbhc_hs_detect(struct snd_soc_component *component,
  826. struct wcd_mbhc_config *mbhc_cfg)
  827. {
  828. struct wcd938x_priv *wcd938x = NULL;
  829. struct wcd938x_mbhc *wcd938x_mbhc = NULL;
  830. if (!component) {
  831. pr_err("%s: component is NULL\n", __func__);
  832. return -EINVAL;
  833. }
  834. wcd938x = snd_soc_component_get_drvdata(component);
  835. if (!wcd938x) {
  836. pr_err("%s: wcd938x is NULL\n", __func__);
  837. return -EINVAL;
  838. }
  839. wcd938x_mbhc = wcd938x->mbhc;
  840. if (!wcd938x_mbhc) {
  841. dev_err(component->dev, "%s: mbhc not initialized!\n", __func__);
  842. return -EINVAL;
  843. }
  844. return wcd_mbhc_start(&wcd938x_mbhc->wcd_mbhc, mbhc_cfg);
  845. }
  846. EXPORT_SYMBOL(wcd938x_mbhc_hs_detect);
  847. /*
  848. * wcd938x_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  849. * @component: handle to snd_soc_component *
  850. */
  851. void wcd938x_mbhc_hs_detect_exit(struct snd_soc_component *component)
  852. {
  853. struct wcd938x_priv *wcd938x = NULL;
  854. struct wcd938x_mbhc *wcd938x_mbhc = NULL;
  855. if (!component) {
  856. pr_err("%s: component is NULL\n", __func__);
  857. return;
  858. }
  859. wcd938x = snd_soc_component_get_drvdata(component);
  860. if (!wcd938x) {
  861. pr_err("%s: wcd938x is NULL\n", __func__);
  862. return;
  863. }
  864. wcd938x_mbhc = wcd938x->mbhc;
  865. if (!wcd938x_mbhc) {
  866. dev_err(component->dev, "%s: mbhc not initialized!\n", __func__);
  867. return;
  868. }
  869. wcd_mbhc_stop(&wcd938x_mbhc->wcd_mbhc);
  870. }
  871. EXPORT_SYMBOL(wcd938x_mbhc_hs_detect_exit);
  872. /*
  873. * wcd938x_mbhc_ssr_down: stop mbhc during
  874. * wcd938x subsystem restart
  875. * mbhc: pointer to wcd937x_mbhc structure
  876. * component: handle to snd_soc_component *
  877. */
  878. void wcd938x_mbhc_ssr_down(struct wcd938x_mbhc *mbhc,
  879. struct snd_soc_component *component)
  880. {
  881. struct wcd_mbhc *wcd_mbhc = NULL;
  882. if (!mbhc || !component)
  883. return;
  884. wcd_mbhc = &mbhc->wcd_mbhc;
  885. if (!wcd_mbhc) {
  886. dev_err(component->dev, "%s: wcd_mbhc is NULL\n", __func__);
  887. return;
  888. }
  889. wcd938x_mbhc_hs_detect_exit(component);
  890. wcd_mbhc_deinit(wcd_mbhc);
  891. }
  892. EXPORT_SYMBOL(wcd938x_mbhc_ssr_down);
  893. /*
  894. * wcd938x_mbhc_post_ssr_init: initialize mbhc for
  895. * wcd938x post subsystem restart
  896. * @mbhc: poniter to wcd938x_mbhc structure
  897. * @component: handle to snd_soc_component *
  898. *
  899. * return 0 if mbhc_init is success or error code in case of failure
  900. */
  901. int wcd938x_mbhc_post_ssr_init(struct wcd938x_mbhc *mbhc,
  902. struct snd_soc_component *component)
  903. {
  904. int ret = 0;
  905. struct wcd_mbhc *wcd_mbhc = NULL;
  906. if (!mbhc || !component)
  907. return -EINVAL;
  908. wcd_mbhc = &mbhc->wcd_mbhc;
  909. if (wcd_mbhc == NULL) {
  910. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  911. return -EINVAL;
  912. }
  913. /* Reset detection type to insertion after SSR recovery */
  914. snd_soc_component_update_bits(component, WCD938X_ANA_MBHC_MECH,
  915. 0x20, 0x20);
  916. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb, &intr_ids,
  917. wcd_mbhc_registers, WCD938X_ZDET_SUPPORTED);
  918. if (ret) {
  919. dev_err(component->dev, "%s: mbhc initialization failed\n",
  920. __func__);
  921. goto done;
  922. }
  923. done:
  924. return ret;
  925. }
  926. EXPORT_SYMBOL(wcd938x_mbhc_post_ssr_init);
  927. /*
  928. * wcd938x_mbhc_init: initialize mbhc for wcd938x
  929. * @mbhc: poniter to wcd938x_mbhc struct pointer to store the configs
  930. * @codec: handle to snd_soc_component *
  931. * @fw_data: handle to firmware data
  932. *
  933. * return 0 if mbhc_init is success or error code in case of failure
  934. */
  935. int wcd938x_mbhc_init(struct wcd938x_mbhc **mbhc,
  936. struct snd_soc_component *component,
  937. struct fw_info *fw_data)
  938. {
  939. struct wcd938x_mbhc *wcd938x_mbhc = NULL;
  940. struct wcd_mbhc *wcd_mbhc = NULL;
  941. int ret = 0;
  942. struct wcd938x_pdata *pdata;
  943. if (!component) {
  944. pr_err("%s: component is NULL\n", __func__);
  945. return -EINVAL;
  946. }
  947. wcd938x_mbhc = devm_kzalloc(component->dev, sizeof(struct wcd938x_mbhc),
  948. GFP_KERNEL);
  949. if (!wcd938x_mbhc)
  950. return -ENOMEM;
  951. wcd938x_mbhc->fw_data = fw_data;
  952. BLOCKING_INIT_NOTIFIER_HEAD(&wcd938x_mbhc->notifier);
  953. wcd_mbhc = &wcd938x_mbhc->wcd_mbhc;
  954. if (wcd_mbhc == NULL) {
  955. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  956. ret = -EINVAL;
  957. goto err;
  958. }
  959. /* Setting default mbhc detection logic to ADC */
  960. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  961. pdata = dev_get_platdata(component->dev);
  962. if (!pdata) {
  963. dev_err(component->dev, "%s: pdata pointer is NULL\n",
  964. __func__);
  965. ret = -EINVAL;
  966. goto err;
  967. }
  968. wcd_mbhc->micb_mv = pdata->micbias.micb2_mv;
  969. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb,
  970. &intr_ids, wcd_mbhc_registers,
  971. WCD938X_ZDET_SUPPORTED);
  972. if (ret) {
  973. dev_err(component->dev, "%s: mbhc initialization failed\n",
  974. __func__);
  975. goto err;
  976. }
  977. (*mbhc) = wcd938x_mbhc;
  978. snd_soc_add_component_controls(component, impedance_detect_controls,
  979. ARRAY_SIZE(impedance_detect_controls));
  980. snd_soc_add_component_controls(component, hph_type_detect_controls,
  981. ARRAY_SIZE(hph_type_detect_controls));
  982. return 0;
  983. err:
  984. devm_kfree(component->dev, wcd938x_mbhc);
  985. return ret;
  986. }
  987. EXPORT_SYMBOL(wcd938x_mbhc_init);
  988. /*
  989. * wcd938x_mbhc_deinit: deinitialize mbhc for wcd938x
  990. * @codec: handle to snd_soc_component *
  991. */
  992. void wcd938x_mbhc_deinit(struct snd_soc_component *component)
  993. {
  994. struct wcd938x_priv *wcd938x;
  995. struct wcd938x_mbhc *wcd938x_mbhc;
  996. if (!component) {
  997. pr_err("%s: component is NULL\n", __func__);
  998. return;
  999. }
  1000. wcd938x = snd_soc_component_get_drvdata(component);
  1001. if (!wcd938x) {
  1002. pr_err("%s: wcd938x is NULL\n", __func__);
  1003. return;
  1004. }
  1005. wcd938x_mbhc = wcd938x->mbhc;
  1006. if (wcd938x_mbhc) {
  1007. wcd_mbhc_deinit(&wcd938x_mbhc->wcd_mbhc);
  1008. devm_kfree(component->dev, wcd938x_mbhc);
  1009. }
  1010. }
  1011. EXPORT_SYMBOL(wcd938x_mbhc_deinit);