wcd9378-mbhc.c 36 KB

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