wcd937x-mbhc.c 34 KB

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