wcd934x-mbhc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/device.h>
  17. #include <linux/printk.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/kernel.h>
  20. #include <linux/gpio.h>
  21. #include <linux/delay.h>
  22. #include <linux/regmap.h>
  23. #include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/soc-dapm.h>
  28. #include "wcd934x.h"
  29. #include "wcd934x-mbhc.h"
  30. #include <asoc/wcd934x_registers.h>
  31. #include "wcd934x_irq.h"
  32. #include "../core.h"
  33. #include "../pdata.h"
  34. #include "../wcd9xxx-irq.h"
  35. #include "../wcdcal-hwdep.h"
  36. #include "../wcd-mbhc-v2-api.h"
  37. #define TAVIL_ZDET_SUPPORTED true
  38. /* Z value defined in milliohm */
  39. #define TAVIL_ZDET_VAL_32 32000
  40. #define TAVIL_ZDET_VAL_400 400000
  41. #define TAVIL_ZDET_VAL_1200 1200000
  42. #define TAVIL_ZDET_VAL_100K 100000000
  43. /* Z floating defined in ohms */
  44. #define TAVIL_ZDET_FLOATING_IMPEDANCE 0x0FFFFFFE
  45. #define TAVIL_ZDET_NUM_MEASUREMENTS 900
  46. #define TAVIL_MBHC_GET_C1(c) ((c & 0xC000) >> 14)
  47. #define TAVIL_MBHC_GET_X1(x) (x & 0x3FFF)
  48. /* Z value compared in milliOhm */
  49. #define TAVIL_MBHC_IS_SECOND_RAMP_REQUIRED(z) ((z > 400000) || (z < 32000))
  50. #define TAVIL_MBHC_ZDET_CONST (86 * 16384)
  51. #define TAVIL_MBHC_MOISTURE_RREF R_24_KOHM
  52. static struct wcd_mbhc_register
  53. wcd_mbhc_registers[WCD_MBHC_REG_FUNC_MAX] = {
  54. WCD_MBHC_REGISTER("WCD_MBHC_L_DET_EN",
  55. WCD934X_ANA_MBHC_MECH, 0x80, 7, 0),
  56. WCD_MBHC_REGISTER("WCD_MBHC_GND_DET_EN",
  57. WCD934X_ANA_MBHC_MECH, 0x40, 6, 0),
  58. WCD_MBHC_REGISTER("WCD_MBHC_MECH_DETECTION_TYPE",
  59. WCD934X_ANA_MBHC_MECH, 0x20, 5, 0),
  60. WCD_MBHC_REGISTER("WCD_MBHC_MIC_CLAMP_CTL",
  61. WCD934X_MBHC_NEW_PLUG_DETECT_CTL, 0x30, 4, 0),
  62. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_DETECTION_TYPE",
  63. WCD934X_ANA_MBHC_ELECT, 0x08, 3, 0),
  64. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_CTRL",
  65. WCD934X_MBHC_NEW_PLUG_DETECT_CTL, 0xC0, 6, 0),
  66. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL",
  67. WCD934X_ANA_MBHC_MECH, 0x04, 2, 0),
  68. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PLUG_TYPE",
  69. WCD934X_ANA_MBHC_MECH, 0x10, 4, 0),
  70. WCD_MBHC_REGISTER("WCD_MBHC_GND_PLUG_TYPE",
  71. WCD934X_ANA_MBHC_MECH, 0x08, 3, 0),
  72. WCD_MBHC_REGISTER("WCD_MBHC_SW_HPH_LP_100K_TO_GND",
  73. WCD934X_ANA_MBHC_MECH, 0x01, 0, 0),
  74. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_SCHMT_ISRC",
  75. WCD934X_ANA_MBHC_ELECT, 0x06, 1, 0),
  76. WCD_MBHC_REGISTER("WCD_MBHC_FSM_EN",
  77. WCD934X_ANA_MBHC_ELECT, 0x80, 7, 0),
  78. WCD_MBHC_REGISTER("WCD_MBHC_INSREM_DBNC",
  79. WCD934X_MBHC_NEW_PLUG_DETECT_CTL, 0x0F, 0, 0),
  80. WCD_MBHC_REGISTER("WCD_MBHC_BTN_DBNC",
  81. WCD934X_MBHC_NEW_CTL_1, 0x03, 0, 0),
  82. WCD_MBHC_REGISTER("WCD_MBHC_HS_VREF",
  83. WCD934X_MBHC_NEW_CTL_2, 0x03, 0, 0),
  84. WCD_MBHC_REGISTER("WCD_MBHC_HS_COMP_RESULT",
  85. WCD934X_ANA_MBHC_RESULT_3, 0x08, 3, 0),
  86. WCD_MBHC_REGISTER("WCD_MBHC_IN2P_CLAMP_STATE",
  87. WCD934X_ANA_MBHC_RESULT_3, 0x10, 4, 0),
  88. WCD_MBHC_REGISTER("WCD_MBHC_MIC_SCHMT_RESULT",
  89. WCD934X_ANA_MBHC_RESULT_3, 0x20, 5, 0),
  90. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_SCHMT_RESULT",
  91. WCD934X_ANA_MBHC_RESULT_3, 0x80, 7, 0),
  92. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_SCHMT_RESULT",
  93. WCD934X_ANA_MBHC_RESULT_3, 0x40, 6, 0),
  94. WCD_MBHC_REGISTER("WCD_MBHC_OCP_FSM_EN",
  95. WCD934X_HPH_OCP_CTL, 0x10, 4, 0),
  96. WCD_MBHC_REGISTER("WCD_MBHC_BTN_RESULT",
  97. WCD934X_ANA_MBHC_RESULT_3, 0x07, 0, 0),
  98. WCD_MBHC_REGISTER("WCD_MBHC_BTN_ISRC_CTL",
  99. WCD934X_ANA_MBHC_ELECT, 0x70, 4, 0),
  100. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_RESULT",
  101. WCD934X_ANA_MBHC_RESULT_3, 0xFF, 0, 0),
  102. WCD_MBHC_REGISTER("WCD_MBHC_MICB_CTRL",
  103. WCD934X_ANA_MICB2, 0xC0, 6, 0),
  104. WCD_MBHC_REGISTER("WCD_MBHC_HPH_CNP_WG_TIME",
  105. WCD934X_HPH_CNP_WG_TIME, 0xFF, 0, 0),
  106. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_PA_EN",
  107. WCD934X_ANA_HPH, 0x40, 6, 0),
  108. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PA_EN",
  109. WCD934X_ANA_HPH, 0x80, 7, 0),
  110. WCD_MBHC_REGISTER("WCD_MBHC_HPH_PA_EN",
  111. WCD934X_ANA_HPH, 0xC0, 6, 0),
  112. WCD_MBHC_REGISTER("WCD_MBHC_SWCH_LEVEL_REMOVE",
  113. WCD934X_ANA_MBHC_RESULT_3, 0x10, 4, 0),
  114. WCD_MBHC_REGISTER("WCD_MBHC_PULLDOWN_CTRL",
  115. 0, 0, 0, 0),
  116. WCD_MBHC_REGISTER("WCD_MBHC_ANC_DET_EN",
  117. WCD934X_MBHC_CTL_BCS, 0x02, 1, 0),
  118. WCD_MBHC_REGISTER("WCD_MBHC_FSM_STATUS",
  119. WCD934X_MBHC_STATUS_SPARE_1, 0x01, 0, 0),
  120. WCD_MBHC_REGISTER("WCD_MBHC_MUX_CTL",
  121. WCD934X_MBHC_NEW_CTL_2, 0x70, 4, 0),
  122. WCD_MBHC_REGISTER("WCD_MBHC_MOISTURE_STATUS",
  123. WCD934X_MBHC_NEW_FSM_STATUS, 0x20, 5, 0),
  124. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_GND",
  125. WCD934X_HPH_PA_CTL2, 0x40, 6, 0),
  126. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_GND",
  127. WCD934X_HPH_PA_CTL2, 0x10, 4, 0),
  128. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_DET_EN",
  129. WCD934X_HPH_L_TEST, 0x01, 0, 0),
  130. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_DET_EN",
  131. WCD934X_HPH_R_TEST, 0x01, 0, 0),
  132. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_STATUS",
  133. WCD934X_INTR_PIN1_STATUS0, 0x04, 2, 0),
  134. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_STATUS",
  135. WCD934X_INTR_PIN1_STATUS0, 0x08, 3, 0),
  136. WCD_MBHC_REGISTER("WCD_MBHC_ADC_EN",
  137. WCD934X_MBHC_NEW_CTL_1, 0x08, 3, 0),
  138. WCD_MBHC_REGISTER("WCD_MBHC_ADC_COMPLETE", WCD934X_MBHC_NEW_FSM_STATUS,
  139. 0x40, 6, 0),
  140. WCD_MBHC_REGISTER("WCD_MBHC_ADC_TIMEOUT", WCD934X_MBHC_NEW_FSM_STATUS,
  141. 0x80, 7, 0),
  142. WCD_MBHC_REGISTER("WCD_MBHC_ADC_RESULT", WCD934X_MBHC_NEW_ADC_RESULT,
  143. 0xFF, 0, 0),
  144. WCD_MBHC_REGISTER("WCD_MBHC_MICB2_VOUT", WCD934X_ANA_MICB2, 0x3F, 0, 0),
  145. WCD_MBHC_REGISTER("WCD_MBHC_ADC_MODE",
  146. WCD934X_MBHC_NEW_CTL_1, 0x10, 4, 0),
  147. WCD_MBHC_REGISTER("WCD_MBHC_DETECTION_DONE",
  148. WCD934X_MBHC_NEW_CTL_1, 0x04, 2, 0),
  149. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_ISRC_EN",
  150. WCD934X_ANA_MBHC_ZDET, 0x02, 1, 0),
  151. };
  152. static const struct wcd_mbhc_intr intr_ids = {
  153. .mbhc_sw_intr = WCD934X_IRQ_MBHC_SW_DET,
  154. .mbhc_btn_press_intr = WCD934X_IRQ_MBHC_BUTTON_PRESS_DET,
  155. .mbhc_btn_release_intr = WCD934X_IRQ_MBHC_BUTTON_RELEASE_DET,
  156. .mbhc_hs_ins_intr = WCD934X_IRQ_MBHC_ELECT_INS_REM_LEG_DET,
  157. .mbhc_hs_rem_intr = WCD934X_IRQ_MBHC_ELECT_INS_REM_DET,
  158. .hph_left_ocp = WCD934X_IRQ_HPH_PA_OCPL_FAULT,
  159. .hph_right_ocp = WCD934X_IRQ_HPH_PA_OCPR_FAULT,
  160. };
  161. static char on_demand_supply_name[][MAX_ON_DEMAND_SUPPLY_NAME_LENGTH] = {
  162. "cdc-vdd-mic-bias",
  163. };
  164. struct tavil_mbhc_zdet_param {
  165. u16 ldo_ctl;
  166. u16 noff;
  167. u16 nshift;
  168. u16 btn5;
  169. u16 btn6;
  170. u16 btn7;
  171. };
  172. static int tavil_mbhc_request_irq(struct snd_soc_codec *codec,
  173. int irq, irq_handler_t handler,
  174. const char *name, void *data)
  175. {
  176. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  177. struct wcd9xxx_core_resource *core_res =
  178. &wcd9xxx->core_res;
  179. return wcd9xxx_request_irq(core_res, irq, handler, name, data);
  180. }
  181. static void tavil_mbhc_irq_control(struct snd_soc_codec *codec,
  182. int irq, bool enable)
  183. {
  184. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  185. struct wcd9xxx_core_resource *core_res =
  186. &wcd9xxx->core_res;
  187. if (enable)
  188. wcd9xxx_enable_irq(core_res, irq);
  189. else
  190. wcd9xxx_disable_irq(core_res, irq);
  191. }
  192. static int tavil_mbhc_free_irq(struct snd_soc_codec *codec,
  193. int irq, void *data)
  194. {
  195. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  196. struct wcd9xxx_core_resource *core_res =
  197. &wcd9xxx->core_res;
  198. wcd9xxx_free_irq(core_res, irq, data);
  199. return 0;
  200. }
  201. static void tavil_mbhc_clk_setup(struct snd_soc_codec *codec,
  202. bool enable)
  203. {
  204. if (enable)
  205. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_1,
  206. 0x80, 0x80);
  207. else
  208. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_1,
  209. 0x80, 0x00);
  210. }
  211. static int tavil_mbhc_btn_to_num(struct snd_soc_codec *codec)
  212. {
  213. return snd_soc_read(codec, WCD934X_ANA_MBHC_RESULT_3) & 0x7;
  214. }
  215. static int tavil_enable_ext_mb_source(struct wcd_mbhc *mbhc,
  216. bool turn_on)
  217. {
  218. struct wcd934x_mbhc *wcd934x_mbhc;
  219. struct snd_soc_codec *codec = mbhc->codec;
  220. struct wcd934x_on_demand_supply *supply;
  221. int ret = 0;
  222. wcd934x_mbhc = container_of(mbhc, struct wcd934x_mbhc, wcd_mbhc);
  223. supply = &wcd934x_mbhc->on_demand_list[WCD934X_ON_DEMAND_MICBIAS];
  224. if (!supply->supply) {
  225. dev_dbg(codec->dev, "%s: warning supply not present ond for %s\n",
  226. __func__, "onDemand Micbias");
  227. return ret;
  228. }
  229. dev_dbg(codec->dev, "%s turn_on: %d count: %d\n", __func__, turn_on,
  230. supply->ondemand_supply_count);
  231. if (turn_on) {
  232. if (!(supply->ondemand_supply_count)) {
  233. ret = snd_soc_dapm_force_enable_pin(
  234. snd_soc_codec_get_dapm(codec),
  235. "MICBIAS_REGULATOR");
  236. snd_soc_dapm_sync(snd_soc_codec_get_dapm(codec));
  237. }
  238. supply->ondemand_supply_count++;
  239. } else {
  240. if (supply->ondemand_supply_count > 0)
  241. supply->ondemand_supply_count--;
  242. if (!(supply->ondemand_supply_count)) {
  243. ret = snd_soc_dapm_disable_pin(
  244. snd_soc_codec_get_dapm(codec),
  245. "MICBIAS_REGULATOR");
  246. snd_soc_dapm_sync(snd_soc_codec_get_dapm(codec));
  247. }
  248. }
  249. if (ret)
  250. dev_err(codec->dev, "%s: Failed to %s external micbias source\n",
  251. __func__, turn_on ? "enable" : "disabled");
  252. else
  253. dev_dbg(codec->dev, "%s: %s external micbias source\n",
  254. __func__, turn_on ? "Enabled" : "Disabled");
  255. return ret;
  256. }
  257. static void tavil_mbhc_mbhc_bias_control(struct snd_soc_codec *codec,
  258. bool enable)
  259. {
  260. if (enable)
  261. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_ELECT,
  262. 0x01, 0x01);
  263. else
  264. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_ELECT,
  265. 0x01, 0x00);
  266. }
  267. static void tavil_mbhc_program_btn_thr(struct snd_soc_codec *codec,
  268. s16 *btn_low, s16 *btn_high,
  269. int num_btn, bool is_micbias)
  270. {
  271. int i;
  272. int vth;
  273. if (num_btn > WCD_MBHC_DEF_BUTTONS) {
  274. dev_err(codec->dev, "%s: invalid number of buttons: %d\n",
  275. __func__, num_btn);
  276. return;
  277. }
  278. /*
  279. * Tavil just needs one set of thresholds for button detection
  280. * due to micbias voltage ramp to pullup upon button press. So
  281. * btn_low and is_micbias are ignored and always program button
  282. * thresholds using btn_high.
  283. */
  284. for (i = 0; i < num_btn; i++) {
  285. vth = ((btn_high[i] * 2) / 25) & 0x3F;
  286. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_BTN0 + i,
  287. 0xFC, vth << 2);
  288. dev_dbg(codec->dev, "%s: btn_high[%d]: %d, vth: %d\n",
  289. __func__, i, btn_high[i], vth);
  290. }
  291. }
  292. static bool tavil_mbhc_lock_sleep(struct wcd_mbhc *mbhc, bool lock)
  293. {
  294. struct snd_soc_codec *codec = mbhc->codec;
  295. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  296. struct wcd9xxx_core_resource *core_res =
  297. &wcd9xxx->core_res;
  298. bool ret = 0;
  299. if (lock)
  300. ret = wcd9xxx_lock_sleep(core_res);
  301. else
  302. wcd9xxx_unlock_sleep(core_res);
  303. return ret;
  304. }
  305. static int tavil_mbhc_register_notifier(struct wcd_mbhc *mbhc,
  306. struct notifier_block *nblock,
  307. bool enable)
  308. {
  309. struct wcd934x_mbhc *wcd934x_mbhc;
  310. wcd934x_mbhc = container_of(mbhc, struct wcd934x_mbhc, wcd_mbhc);
  311. if (enable)
  312. return blocking_notifier_chain_register(&wcd934x_mbhc->notifier,
  313. nblock);
  314. else
  315. return blocking_notifier_chain_unregister(
  316. &wcd934x_mbhc->notifier, nblock);
  317. }
  318. static bool tavil_mbhc_micb_en_status(struct wcd_mbhc *mbhc, int micb_num)
  319. {
  320. u8 val;
  321. if (micb_num == MIC_BIAS_2) {
  322. val = (snd_soc_read(mbhc->codec, WCD934X_ANA_MICB2) >> 6);
  323. if (val == 0x01)
  324. return true;
  325. }
  326. return false;
  327. }
  328. static bool tavil_mbhc_hph_pa_on_status(struct snd_soc_codec *codec)
  329. {
  330. return (snd_soc_read(codec, WCD934X_ANA_HPH) & 0xC0) ? true : false;
  331. }
  332. static void tavil_mbhc_hph_l_pull_up_control(
  333. struct snd_soc_codec *codec,
  334. enum mbhc_hs_pullup_iref pull_up_cur)
  335. {
  336. /* Default pull up current to 2uA */
  337. if (pull_up_cur < I_OFF || pull_up_cur > I_3P0_UA ||
  338. pull_up_cur == I_DEFAULT)
  339. pull_up_cur = I_2P0_UA;
  340. dev_dbg(codec->dev, "%s: HS pull up current:%d\n",
  341. __func__, pull_up_cur);
  342. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_PLUG_DETECT_CTL,
  343. 0xC0, pull_up_cur << 6);
  344. }
  345. static int tavil_mbhc_request_micbias(struct snd_soc_codec *codec,
  346. int micb_num, int req)
  347. {
  348. int ret;
  349. /*
  350. * If micbias is requested, make sure that there
  351. * is vote to enable mclk
  352. */
  353. if (req == MICB_ENABLE)
  354. tavil_cdc_mclk_enable(codec, true);
  355. ret = tavil_micbias_control(codec, micb_num, req, false);
  356. /*
  357. * Release vote for mclk while requesting for
  358. * micbias disable
  359. */
  360. if (req == MICB_DISABLE)
  361. tavil_cdc_mclk_enable(codec, false);
  362. return ret;
  363. }
  364. static void tavil_mbhc_micb_ramp_control(struct snd_soc_codec *codec,
  365. bool enable)
  366. {
  367. if (enable) {
  368. snd_soc_update_bits(codec, WCD934X_ANA_MICB2_RAMP,
  369. 0x1C, 0x0C);
  370. snd_soc_update_bits(codec, WCD934X_ANA_MICB2_RAMP,
  371. 0x80, 0x80);
  372. } else {
  373. snd_soc_update_bits(codec, WCD934X_ANA_MICB2_RAMP,
  374. 0x80, 0x00);
  375. snd_soc_update_bits(codec, WCD934X_ANA_MICB2_RAMP,
  376. 0x1C, 0x00);
  377. }
  378. }
  379. static struct firmware_cal *tavil_get_hwdep_fw_cal(struct wcd_mbhc *mbhc,
  380. enum wcd_cal_type type)
  381. {
  382. struct wcd934x_mbhc *wcd934x_mbhc;
  383. struct firmware_cal *hwdep_cal;
  384. struct snd_soc_codec *codec = mbhc->codec;
  385. wcd934x_mbhc = container_of(mbhc, struct wcd934x_mbhc, wcd_mbhc);
  386. if (!codec) {
  387. pr_err("%s: NULL codec pointer\n", __func__);
  388. return NULL;
  389. }
  390. hwdep_cal = wcdcal_get_fw_cal(wcd934x_mbhc->fw_data, type);
  391. if (!hwdep_cal)
  392. dev_err(codec->dev, "%s: cal not sent by %d\n",
  393. __func__, type);
  394. return hwdep_cal;
  395. }
  396. static int tavil_mbhc_micb_ctrl_threshold_mic(struct snd_soc_codec *codec,
  397. int micb_num, bool req_en)
  398. {
  399. struct wcd9xxx_pdata *pdata = dev_get_platdata(codec->dev->parent);
  400. int rc, micb_mv;
  401. if (micb_num != MIC_BIAS_2)
  402. return -EINVAL;
  403. /*
  404. * If device tree micbias level is already above the minimum
  405. * voltage needed to detect threshold microphone, then do
  406. * not change the micbias, just return.
  407. */
  408. if (pdata->micbias.micb2_mv >= WCD_MBHC_THR_HS_MICB_MV)
  409. return 0;
  410. micb_mv = req_en ? WCD_MBHC_THR_HS_MICB_MV : pdata->micbias.micb2_mv;
  411. rc = tavil_mbhc_micb_adjust_voltage(codec, micb_mv, MIC_BIAS_2);
  412. return rc;
  413. }
  414. static inline void tavil_mbhc_get_result_params(struct wcd9xxx *wcd9xxx,
  415. s16 *d1_a, u16 noff,
  416. int32_t *zdet)
  417. {
  418. int i;
  419. int val, val1;
  420. s16 c1;
  421. s32 x1, d1;
  422. int32_t denom;
  423. int minCode_param[] = {
  424. 3277, 1639, 820, 410, 205, 103, 52, 26
  425. };
  426. regmap_update_bits(wcd9xxx->regmap, WCD934X_ANA_MBHC_ZDET, 0x20, 0x20);
  427. for (i = 0; i < TAVIL_ZDET_NUM_MEASUREMENTS; i++) {
  428. regmap_read(wcd9xxx->regmap, WCD934X_ANA_MBHC_RESULT_2, &val);
  429. if (val & 0x80)
  430. break;
  431. }
  432. val = val << 0x8;
  433. regmap_read(wcd9xxx->regmap, WCD934X_ANA_MBHC_RESULT_1, &val1);
  434. val |= val1;
  435. regmap_update_bits(wcd9xxx->regmap, WCD934X_ANA_MBHC_ZDET, 0x20, 0x00);
  436. x1 = TAVIL_MBHC_GET_X1(val);
  437. c1 = TAVIL_MBHC_GET_C1(val);
  438. /* If ramp is not complete, give additional 5ms */
  439. if ((c1 < 2) && x1)
  440. usleep_range(5000, 5050);
  441. if (!c1 || !x1) {
  442. dev_dbg(wcd9xxx->dev,
  443. "%s: Impedance detect ramp error, c1=%d, x1=0x%x\n",
  444. __func__, c1, x1);
  445. goto ramp_down;
  446. }
  447. d1 = d1_a[c1];
  448. denom = (x1 * d1) - (1 << (14 - noff));
  449. if (denom > 0)
  450. *zdet = (TAVIL_MBHC_ZDET_CONST * 1000) / denom;
  451. else if (x1 < minCode_param[noff])
  452. *zdet = TAVIL_ZDET_FLOATING_IMPEDANCE;
  453. dev_dbg(wcd9xxx->dev, "%s: d1=%d, c1=%d, x1=0x%x, z_val=%d(milliOhm)\n",
  454. __func__, d1, c1, x1, *zdet);
  455. ramp_down:
  456. i = 0;
  457. while (x1) {
  458. regmap_bulk_read(wcd9xxx->regmap,
  459. WCD934X_ANA_MBHC_RESULT_1, (u8 *)&val, 2);
  460. x1 = TAVIL_MBHC_GET_X1(val);
  461. i++;
  462. if (i == TAVIL_ZDET_NUM_MEASUREMENTS)
  463. break;
  464. }
  465. }
  466. static void tavil_mbhc_zdet_ramp(struct snd_soc_codec *codec,
  467. struct tavil_mbhc_zdet_param *zdet_param,
  468. int32_t *zl, int32_t *zr, s16 *d1_a)
  469. {
  470. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  471. int32_t zdet = 0;
  472. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_ZDET_ANA_CTL, 0x70,
  473. zdet_param->ldo_ctl << 4);
  474. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_BTN5, 0xFC,
  475. zdet_param->btn5);
  476. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_BTN6, 0xFC,
  477. zdet_param->btn6);
  478. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_BTN7, 0xFC,
  479. zdet_param->btn7);
  480. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_ZDET_ANA_CTL, 0x0F,
  481. zdet_param->noff);
  482. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_ZDET_RAMP_CTL, 0x0F,
  483. zdet_param->nshift);
  484. if (!zl)
  485. goto z_right;
  486. /* Start impedance measurement for HPH_L */
  487. regmap_update_bits(wcd9xxx->regmap,
  488. WCD934X_ANA_MBHC_ZDET, 0x80, 0x80);
  489. dev_dbg(wcd9xxx->dev, "%s: ramp for HPH_L, noff = %d\n",
  490. __func__, zdet_param->noff);
  491. tavil_mbhc_get_result_params(wcd9xxx, d1_a, zdet_param->noff, &zdet);
  492. regmap_update_bits(wcd9xxx->regmap,
  493. WCD934X_ANA_MBHC_ZDET, 0x80, 0x00);
  494. *zl = zdet;
  495. z_right:
  496. if (!zr)
  497. return;
  498. /* Start impedance measurement for HPH_R */
  499. regmap_update_bits(wcd9xxx->regmap,
  500. WCD934X_ANA_MBHC_ZDET, 0x40, 0x40);
  501. dev_dbg(wcd9xxx->dev, "%s: ramp for HPH_R, noff = %d\n",
  502. __func__, zdet_param->noff);
  503. tavil_mbhc_get_result_params(wcd9xxx, d1_a, zdet_param->noff, &zdet);
  504. regmap_update_bits(wcd9xxx->regmap,
  505. WCD934X_ANA_MBHC_ZDET, 0x40, 0x00);
  506. *zr = zdet;
  507. }
  508. static inline void tavil_wcd_mbhc_qfuse_cal(struct snd_soc_codec *codec,
  509. int32_t *z_val, int flag_l_r)
  510. {
  511. s16 q1;
  512. int q1_cal;
  513. if (*z_val < (TAVIL_ZDET_VAL_400/1000))
  514. q1 = snd_soc_read(codec,
  515. WCD934X_CHIP_TIER_CTRL_EFUSE_VAL_OUT1 + (2 * flag_l_r));
  516. else
  517. q1 = snd_soc_read(codec,
  518. WCD934X_CHIP_TIER_CTRL_EFUSE_VAL_OUT2 + (2 * flag_l_r));
  519. if (q1 & 0x80)
  520. q1_cal = (10000 - ((q1 & 0x7F) * 25));
  521. else
  522. q1_cal = (10000 + (q1 * 25));
  523. if (q1_cal > 0)
  524. *z_val = ((*z_val) * 10000) / q1_cal;
  525. }
  526. static void tavil_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
  527. uint32_t *zr)
  528. {
  529. struct snd_soc_codec *codec = mbhc->codec;
  530. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  531. s16 reg0, reg1, reg2, reg3, reg4;
  532. int32_t z1L, z1R, z1Ls;
  533. int zMono, z_diff1, z_diff2;
  534. bool is_fsm_disable = false;
  535. struct tavil_mbhc_zdet_param zdet_param[] = {
  536. {4, 0, 4, 0x08, 0x14, 0x18}, /* < 32ohm */
  537. {2, 0, 3, 0x18, 0x7C, 0x90}, /* 32ohm < Z < 400ohm */
  538. {1, 4, 5, 0x18, 0x7C, 0x90}, /* 400ohm < Z < 1200ohm */
  539. {1, 6, 7, 0x18, 0x7C, 0x90}, /* >1200ohm */
  540. };
  541. struct tavil_mbhc_zdet_param *zdet_param_ptr = NULL;
  542. s16 d1_a[][4] = {
  543. {0, 30, 90, 30},
  544. {0, 30, 30, 5},
  545. {0, 30, 30, 5},
  546. {0, 30, 30, 5},
  547. };
  548. s16 *d1 = NULL;
  549. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  550. reg0 = snd_soc_read(codec, WCD934X_ANA_MBHC_BTN5);
  551. reg1 = snd_soc_read(codec, WCD934X_ANA_MBHC_BTN6);
  552. reg2 = snd_soc_read(codec, WCD934X_ANA_MBHC_BTN7);
  553. reg3 = snd_soc_read(codec, WCD934X_MBHC_CTL_CLK);
  554. reg4 = snd_soc_read(codec, WCD934X_MBHC_NEW_ZDET_ANA_CTL);
  555. if (snd_soc_read(codec, WCD934X_ANA_MBHC_ELECT) & 0x80) {
  556. is_fsm_disable = true;
  557. regmap_update_bits(wcd9xxx->regmap,
  558. WCD934X_ANA_MBHC_ELECT, 0x80, 0x00);
  559. }
  560. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  561. if (mbhc->hphl_swh)
  562. regmap_update_bits(wcd9xxx->regmap,
  563. WCD934X_ANA_MBHC_MECH, 0x80, 0x00);
  564. /* Turn off 100k pull down on HPHL */
  565. regmap_update_bits(wcd9xxx->regmap,
  566. WCD934X_ANA_MBHC_MECH, 0x01, 0x00);
  567. /* First get impedance on Left */
  568. d1 = d1_a[1];
  569. zdet_param_ptr = &zdet_param[1];
  570. tavil_mbhc_zdet_ramp(codec, zdet_param_ptr, &z1L, NULL, d1);
  571. if (!TAVIL_MBHC_IS_SECOND_RAMP_REQUIRED(z1L))
  572. goto left_ch_impedance;
  573. /* Second ramp for left ch */
  574. if (z1L < TAVIL_ZDET_VAL_32) {
  575. zdet_param_ptr = &zdet_param[0];
  576. d1 = d1_a[0];
  577. } else if ((z1L > TAVIL_ZDET_VAL_400) && (z1L <= TAVIL_ZDET_VAL_1200)) {
  578. zdet_param_ptr = &zdet_param[2];
  579. d1 = d1_a[2];
  580. } else if (z1L > TAVIL_ZDET_VAL_1200) {
  581. zdet_param_ptr = &zdet_param[3];
  582. d1 = d1_a[3];
  583. }
  584. tavil_mbhc_zdet_ramp(codec, zdet_param_ptr, &z1L, NULL, d1);
  585. left_ch_impedance:
  586. if ((z1L == TAVIL_ZDET_FLOATING_IMPEDANCE) ||
  587. (z1L > TAVIL_ZDET_VAL_100K)) {
  588. *zl = TAVIL_ZDET_FLOATING_IMPEDANCE;
  589. zdet_param_ptr = &zdet_param[1];
  590. d1 = d1_a[1];
  591. } else {
  592. *zl = z1L/1000;
  593. tavil_wcd_mbhc_qfuse_cal(codec, zl, 0);
  594. }
  595. dev_dbg(codec->dev, "%s: impedance on HPH_L = %d(ohms)\n",
  596. __func__, *zl);
  597. /* Start of right impedance ramp and calculation */
  598. tavil_mbhc_zdet_ramp(codec, zdet_param_ptr, NULL, &z1R, d1);
  599. if (TAVIL_MBHC_IS_SECOND_RAMP_REQUIRED(z1R)) {
  600. if (((z1R > TAVIL_ZDET_VAL_1200) &&
  601. (zdet_param_ptr->noff == 0x6)) ||
  602. ((*zl) != TAVIL_ZDET_FLOATING_IMPEDANCE))
  603. goto right_ch_impedance;
  604. /* Second ramp for right ch */
  605. if (z1R < TAVIL_ZDET_VAL_32) {
  606. zdet_param_ptr = &zdet_param[0];
  607. d1 = d1_a[0];
  608. } else if ((z1R > TAVIL_ZDET_VAL_400) &&
  609. (z1R <= TAVIL_ZDET_VAL_1200)) {
  610. zdet_param_ptr = &zdet_param[2];
  611. d1 = d1_a[2];
  612. } else if (z1R > TAVIL_ZDET_VAL_1200) {
  613. zdet_param_ptr = &zdet_param[3];
  614. d1 = d1_a[3];
  615. }
  616. tavil_mbhc_zdet_ramp(codec, zdet_param_ptr, NULL, &z1R, d1);
  617. }
  618. right_ch_impedance:
  619. if ((z1R == TAVIL_ZDET_FLOATING_IMPEDANCE) ||
  620. (z1R > TAVIL_ZDET_VAL_100K)) {
  621. *zr = TAVIL_ZDET_FLOATING_IMPEDANCE;
  622. } else {
  623. *zr = z1R/1000;
  624. tavil_wcd_mbhc_qfuse_cal(codec, zr, 1);
  625. }
  626. dev_dbg(codec->dev, "%s: impedance on HPH_R = %d(ohms)\n",
  627. __func__, *zr);
  628. /* Mono/stereo detection */
  629. if ((*zl == TAVIL_ZDET_FLOATING_IMPEDANCE) &&
  630. (*zr == TAVIL_ZDET_FLOATING_IMPEDANCE)) {
  631. dev_dbg(codec->dev,
  632. "%s: plug type is invalid or extension cable\n",
  633. __func__);
  634. goto zdet_complete;
  635. }
  636. if ((*zl == TAVIL_ZDET_FLOATING_IMPEDANCE) ||
  637. (*zr == TAVIL_ZDET_FLOATING_IMPEDANCE) ||
  638. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  639. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  640. dev_dbg(codec->dev,
  641. "%s: Mono plug type with one ch floating or shorted to GND\n",
  642. __func__);
  643. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  644. goto zdet_complete;
  645. }
  646. snd_soc_update_bits(codec, WCD934X_HPH_R_ATEST, 0x02, 0x02);
  647. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2, 0x40, 0x01);
  648. if (*zl < (TAVIL_ZDET_VAL_32/1000))
  649. tavil_mbhc_zdet_ramp(codec, &zdet_param[0], &z1Ls, NULL, d1);
  650. else
  651. tavil_mbhc_zdet_ramp(codec, &zdet_param[1], &z1Ls, NULL, d1);
  652. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2, 0x40, 0x00);
  653. snd_soc_update_bits(codec, WCD934X_HPH_R_ATEST, 0x02, 0x00);
  654. z1Ls /= 1000;
  655. tavil_wcd_mbhc_qfuse_cal(codec, &z1Ls, 0);
  656. /* Parallel of left Z and 9 ohm pull down resistor */
  657. zMono = ((*zl) * 9) / ((*zl) + 9);
  658. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  659. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  660. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  661. dev_dbg(codec->dev, "%s: stereo plug type detected\n",
  662. __func__);
  663. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  664. } else {
  665. dev_dbg(codec->dev, "%s: MONO plug type detected\n",
  666. __func__);
  667. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  668. }
  669. zdet_complete:
  670. snd_soc_write(codec, WCD934X_ANA_MBHC_BTN5, reg0);
  671. snd_soc_write(codec, WCD934X_ANA_MBHC_BTN6, reg1);
  672. snd_soc_write(codec, WCD934X_ANA_MBHC_BTN7, reg2);
  673. /* Turn on 100k pull down on HPHL */
  674. regmap_update_bits(wcd9xxx->regmap,
  675. WCD934X_ANA_MBHC_MECH, 0x01, 0x01);
  676. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  677. if (mbhc->hphl_swh)
  678. regmap_update_bits(wcd9xxx->regmap,
  679. WCD934X_ANA_MBHC_MECH, 0x80, 0x80);
  680. snd_soc_write(codec, WCD934X_MBHC_NEW_ZDET_ANA_CTL, reg4);
  681. snd_soc_write(codec, WCD934X_MBHC_CTL_CLK, reg3);
  682. if (is_fsm_disable)
  683. regmap_update_bits(wcd9xxx->regmap,
  684. WCD934X_ANA_MBHC_ELECT, 0x80, 0x80);
  685. }
  686. static void tavil_mbhc_gnd_det_ctrl(struct snd_soc_codec *codec, bool enable)
  687. {
  688. if (enable) {
  689. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_MECH,
  690. 0x02, 0x02);
  691. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_MECH,
  692. 0x40, 0x40);
  693. } else {
  694. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_MECH,
  695. 0x40, 0x00);
  696. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_MECH,
  697. 0x02, 0x00);
  698. }
  699. }
  700. static void tavil_mbhc_hph_pull_down_ctrl(struct snd_soc_codec *codec,
  701. bool enable)
  702. {
  703. if (enable) {
  704. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2,
  705. 0x40, 0x40);
  706. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2,
  707. 0x10, 0x10);
  708. } else {
  709. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2,
  710. 0x40, 0x00);
  711. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2,
  712. 0x10, 0x00);
  713. }
  714. }
  715. static void tavil_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  716. {
  717. struct snd_soc_codec *codec = mbhc->codec;
  718. if ((mbhc->moist_rref == R_OFF) ||
  719. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  720. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_2,
  721. 0x0C, R_OFF << 2);
  722. return;
  723. }
  724. /* Donot enable moisture detection if jack type is NC */
  725. if (!mbhc->hphl_swh) {
  726. dev_dbg(codec->dev, "%s: disable moisture detection for NC\n",
  727. __func__);
  728. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_2,
  729. 0x0C, R_OFF << 2);
  730. return;
  731. }
  732. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_2,
  733. 0x0C, mbhc->moist_rref << 2);
  734. }
  735. static bool tavil_hph_register_recovery(struct wcd_mbhc *mbhc)
  736. {
  737. struct snd_soc_codec *codec = mbhc->codec;
  738. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  739. if (!wcd934x_mbhc)
  740. return false;
  741. wcd934x_mbhc->is_hph_recover = false;
  742. snd_soc_dapm_force_enable_pin(snd_soc_codec_get_dapm(codec),
  743. "RESET_HPH_REGISTERS");
  744. snd_soc_dapm_sync(snd_soc_codec_get_dapm(codec));
  745. snd_soc_dapm_disable_pin(snd_soc_codec_get_dapm(codec),
  746. "RESET_HPH_REGISTERS");
  747. snd_soc_dapm_sync(snd_soc_codec_get_dapm(codec));
  748. return wcd934x_mbhc->is_hph_recover;
  749. }
  750. static void tavil_update_anc_state(struct snd_soc_codec *codec, bool enable,
  751. int anc_num)
  752. {
  753. if (enable)
  754. snd_soc_update_bits(codec, WCD934X_CDC_RX1_RX_PATH_CFG0 +
  755. (20 * anc_num), 0x10, 0x10);
  756. else
  757. snd_soc_update_bits(codec, WCD934X_CDC_RX1_RX_PATH_CFG0 +
  758. (20 * anc_num), 0x10, 0x00);
  759. }
  760. static bool tavil_is_anc_on(struct wcd_mbhc *mbhc)
  761. {
  762. bool anc_on = false;
  763. u16 ancl, ancr;
  764. ancl =
  765. (snd_soc_read(mbhc->codec, WCD934X_CDC_RX1_RX_PATH_CFG0)) & 0x10;
  766. ancr =
  767. (snd_soc_read(mbhc->codec, WCD934X_CDC_RX2_RX_PATH_CFG0)) & 0x10;
  768. anc_on = !!(ancl | ancr);
  769. return anc_on;
  770. }
  771. static const struct wcd_mbhc_cb mbhc_cb = {
  772. .request_irq = tavil_mbhc_request_irq,
  773. .irq_control = tavil_mbhc_irq_control,
  774. .free_irq = tavil_mbhc_free_irq,
  775. .clk_setup = tavil_mbhc_clk_setup,
  776. .map_btn_code_to_num = tavil_mbhc_btn_to_num,
  777. .enable_mb_source = tavil_enable_ext_mb_source,
  778. .mbhc_bias = tavil_mbhc_mbhc_bias_control,
  779. .set_btn_thr = tavil_mbhc_program_btn_thr,
  780. .lock_sleep = tavil_mbhc_lock_sleep,
  781. .register_notifier = tavil_mbhc_register_notifier,
  782. .micbias_enable_status = tavil_mbhc_micb_en_status,
  783. .hph_pa_on_status = tavil_mbhc_hph_pa_on_status,
  784. .hph_pull_up_control = tavil_mbhc_hph_l_pull_up_control,
  785. .mbhc_micbias_control = tavil_mbhc_request_micbias,
  786. .mbhc_micb_ramp_control = tavil_mbhc_micb_ramp_control,
  787. .get_hwdep_fw_cal = tavil_get_hwdep_fw_cal,
  788. .mbhc_micb_ctrl_thr_mic = tavil_mbhc_micb_ctrl_threshold_mic,
  789. .compute_impedance = tavil_wcd_mbhc_calc_impedance,
  790. .mbhc_gnd_det_ctrl = tavil_mbhc_gnd_det_ctrl,
  791. .hph_pull_down_ctrl = tavil_mbhc_hph_pull_down_ctrl,
  792. .mbhc_moisture_config = tavil_mbhc_moisture_config,
  793. .hph_register_recovery = tavil_hph_register_recovery,
  794. .update_anc_state = tavil_update_anc_state,
  795. .is_anc_on = tavil_is_anc_on,
  796. };
  797. static struct regulator *tavil_codec_find_ondemand_regulator(
  798. struct snd_soc_codec *codec, const char *name)
  799. {
  800. int i;
  801. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  802. struct wcd9xxx_pdata *pdata = dev_get_platdata(codec->dev->parent);
  803. for (i = 0; i < wcd9xxx->num_of_supplies; ++i) {
  804. if (pdata->regulator[i].ondemand &&
  805. wcd9xxx->supplies[i].supply &&
  806. !strcmp(wcd9xxx->supplies[i].supply, name))
  807. return wcd9xxx->supplies[i].consumer;
  808. }
  809. dev_dbg(codec->dev, "Warning: regulator not found:%s\n",
  810. name);
  811. return NULL;
  812. }
  813. static int tavil_get_hph_type(struct snd_kcontrol *kcontrol,
  814. struct snd_ctl_elem_value *ucontrol)
  815. {
  816. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  817. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  818. struct wcd_mbhc *mbhc;
  819. if (!wcd934x_mbhc) {
  820. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  821. return -EINVAL;
  822. }
  823. mbhc = &wcd934x_mbhc->wcd_mbhc;
  824. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  825. dev_dbg(codec->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  826. return 0;
  827. }
  828. static int tavil_hph_impedance_get(struct snd_kcontrol *kcontrol,
  829. struct snd_ctl_elem_value *ucontrol)
  830. {
  831. uint32_t zl, zr;
  832. bool hphr;
  833. struct soc_multi_mixer_control *mc;
  834. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  835. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  836. if (!wcd934x_mbhc) {
  837. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  838. return -EINVAL;
  839. }
  840. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  841. hphr = mc->shift;
  842. wcd_mbhc_get_impedance(&wcd934x_mbhc->wcd_mbhc, &zl, &zr);
  843. dev_dbg(codec->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  844. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  845. return 0;
  846. }
  847. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  848. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  849. tavil_get_hph_type, NULL),
  850. };
  851. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  852. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  853. tavil_hph_impedance_get, NULL),
  854. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  855. tavil_hph_impedance_get, NULL),
  856. };
  857. /*
  858. * tavil_mbhc_get_impedance: get impedance of headphone left and right channels
  859. * @wcd934x_mbhc: handle to struct wcd934x_mbhc *
  860. * @zl: handle to left-ch impedance
  861. * @zr: handle to right-ch impedance
  862. * return 0 for success or error code in case of failure
  863. */
  864. int tavil_mbhc_get_impedance(struct wcd934x_mbhc *wcd934x_mbhc,
  865. uint32_t *zl, uint32_t *zr)
  866. {
  867. if (!wcd934x_mbhc) {
  868. pr_err("%s: mbhc not initialized!\n", __func__);
  869. return -EINVAL;
  870. }
  871. if (!zl || !zr) {
  872. pr_err("%s: zl or zr null!\n", __func__);
  873. return -EINVAL;
  874. }
  875. return wcd_mbhc_get_impedance(&wcd934x_mbhc->wcd_mbhc, zl, zr);
  876. }
  877. EXPORT_SYMBOL(tavil_mbhc_get_impedance);
  878. /*
  879. * tavil_mbhc_hs_detect: starts mbhc insertion/removal functionality
  880. * @codec: handle to snd_soc_codec *
  881. * @mbhc_cfg: handle to mbhc configuration structure
  882. * return 0 if mbhc_start is success or error code in case of failure
  883. */
  884. int tavil_mbhc_hs_detect(struct snd_soc_codec *codec,
  885. struct wcd_mbhc_config *mbhc_cfg)
  886. {
  887. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  888. if (!wcd934x_mbhc) {
  889. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  890. return -EINVAL;
  891. }
  892. return wcd_mbhc_start(&wcd934x_mbhc->wcd_mbhc, mbhc_cfg);
  893. }
  894. EXPORT_SYMBOL(tavil_mbhc_hs_detect);
  895. /*
  896. * tavil_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  897. * @codec: handle to snd_soc_codec *
  898. */
  899. void tavil_mbhc_hs_detect_exit(struct snd_soc_codec *codec)
  900. {
  901. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  902. if (!wcd934x_mbhc) {
  903. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  904. return;
  905. }
  906. wcd_mbhc_stop(&wcd934x_mbhc->wcd_mbhc);
  907. }
  908. EXPORT_SYMBOL(tavil_mbhc_hs_detect_exit);
  909. /*
  910. * tavil_mbhc_post_ssr_init: initialize mbhc for tavil post subsystem restart
  911. * @mbhc: poniter to wcd934x_mbhc structure
  912. * @codec: handle to snd_soc_codec *
  913. *
  914. * return 0 if mbhc_init is success or error code in case of failure
  915. */
  916. int tavil_mbhc_post_ssr_init(struct wcd934x_mbhc *mbhc,
  917. struct snd_soc_codec *codec)
  918. {
  919. int ret;
  920. struct wcd_mbhc *wcd_mbhc;
  921. if (!mbhc || !codec)
  922. return -EINVAL;
  923. wcd_mbhc = &mbhc->wcd_mbhc;
  924. if (wcd_mbhc == NULL) {
  925. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  926. return -EINVAL;
  927. }
  928. tavil_mbhc_hs_detect_exit(codec);
  929. wcd_mbhc_deinit(wcd_mbhc);
  930. ret = wcd_mbhc_init(wcd_mbhc, codec, &mbhc_cb, &intr_ids,
  931. wcd_mbhc_registers, TAVIL_ZDET_SUPPORTED);
  932. if (ret) {
  933. dev_err(codec->dev, "%s: mbhc initialization failed\n",
  934. __func__);
  935. goto done;
  936. }
  937. if (wcd_mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY) {
  938. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_1, 0x04, 0x04);
  939. snd_soc_update_bits(codec, WCD934X_MBHC_CTL_BCS, 0x01, 0x01);
  940. }
  941. done:
  942. return ret;
  943. }
  944. EXPORT_SYMBOL(tavil_mbhc_post_ssr_init);
  945. /*
  946. * tavil_mbhc_init: initialize mbhc for tavil
  947. * @mbhc: poniter to wcd934x_mbhc struct pointer to store the configs
  948. * @codec: handle to snd_soc_codec *
  949. * @fw_data: handle to firmware data
  950. *
  951. * return 0 if mbhc_init is success or error code in case of failure
  952. */
  953. int tavil_mbhc_init(struct wcd934x_mbhc **mbhc, struct snd_soc_codec *codec,
  954. struct fw_info *fw_data)
  955. {
  956. struct regulator *supply;
  957. struct wcd934x_mbhc *wcd934x_mbhc;
  958. struct wcd_mbhc *wcd_mbhc;
  959. int ret;
  960. wcd934x_mbhc = devm_kzalloc(codec->dev, sizeof(struct wcd934x_mbhc),
  961. GFP_KERNEL);
  962. if (!wcd934x_mbhc)
  963. return -ENOMEM;
  964. wcd934x_mbhc->wcd9xxx = dev_get_drvdata(codec->dev->parent);
  965. wcd934x_mbhc->fw_data = fw_data;
  966. BLOCKING_INIT_NOTIFIER_HEAD(&wcd934x_mbhc->notifier);
  967. wcd_mbhc = &wcd934x_mbhc->wcd_mbhc;
  968. if (wcd_mbhc == NULL) {
  969. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  970. ret = -EINVAL;
  971. goto err;
  972. }
  973. /* Setting default mbhc detection logic to ADC for Tavil */
  974. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  975. ret = wcd_mbhc_init(wcd_mbhc, codec, &mbhc_cb,
  976. &intr_ids, wcd_mbhc_registers,
  977. TAVIL_ZDET_SUPPORTED);
  978. if (ret) {
  979. dev_err(codec->dev, "%s: mbhc initialization failed\n",
  980. __func__);
  981. goto err;
  982. }
  983. supply = tavil_codec_find_ondemand_regulator(codec,
  984. on_demand_supply_name[WCD934X_ON_DEMAND_MICBIAS]);
  985. if (supply) {
  986. wcd934x_mbhc->on_demand_list[
  987. WCD934X_ON_DEMAND_MICBIAS].supply =
  988. supply;
  989. wcd934x_mbhc->on_demand_list[
  990. WCD934X_ON_DEMAND_MICBIAS].ondemand_supply_count =
  991. 0;
  992. }
  993. (*mbhc) = wcd934x_mbhc;
  994. snd_soc_add_codec_controls(codec, impedance_detect_controls,
  995. ARRAY_SIZE(impedance_detect_controls));
  996. snd_soc_add_codec_controls(codec, hph_type_detect_controls,
  997. ARRAY_SIZE(hph_type_detect_controls));
  998. if (wcd_mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY) {
  999. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_1, 0x04, 0x04);
  1000. snd_soc_update_bits(codec, WCD934X_MBHC_CTL_BCS, 0x01, 0x01);
  1001. }
  1002. return 0;
  1003. err:
  1004. devm_kfree(codec->dev, wcd934x_mbhc);
  1005. return ret;
  1006. }
  1007. EXPORT_SYMBOL(tavil_mbhc_init);
  1008. /*
  1009. * tavil_mbhc_deinit: deinitialize mbhc for tavil
  1010. * @codec: handle to snd_soc_codec *
  1011. */
  1012. void tavil_mbhc_deinit(struct snd_soc_codec *codec)
  1013. {
  1014. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  1015. if (wcd934x_mbhc) {
  1016. wcd_mbhc_deinit(&wcd934x_mbhc->wcd_mbhc);
  1017. devm_kfree(codec->dev, wcd934x_mbhc);
  1018. }
  1019. }
  1020. EXPORT_SYMBOL(tavil_mbhc_deinit);