wcd938x-mbhc.c 35 KB

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