wcd9378-mbhc.c 35 KB

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