wcd939x-mbhc.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/device.h>
  10. #include <linux/printk.h>
  11. #include <linux/ratelimit.h>
  12. #include <linux/kernel.h>
  13. #include <linux/gpio.h>
  14. #include <linux/delay.h>
  15. #include <linux/regmap.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include <sound/soc-dapm.h>
  20. #include <asoc/wcdcal-hwdep.h>
  21. #include <asoc/wcd-mbhc-v2-api.h>
  22. #include "wcd939x-registers.h"
  23. #include "internal.h"
  24. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  25. #include <linux/soc/qcom/wcd939x-i2c.h>
  26. #endif
  27. #define WCD939X_ZDET_SUPPORTED true
  28. /* Z value defined in milliohm */
  29. #define WCD939X_ZDET_VAL_32 32000
  30. #define WCD939X_ZDET_VAL_400 400000
  31. #define WCD939X_ZDET_VAL_1200 1200000
  32. #define WCD939X_ZDET_VAL_100K 100000000
  33. /* Z floating defined in ohms */
  34. #define WCD939X_ZDET_FLOATING_IMPEDANCE 0x0FFFFFFE
  35. #define WCD939X_ZDET_NUM_MEASUREMENTS 900
  36. #define WCD939X_MBHC_GET_C1(c) ((c & 0xC000) >> 14)
  37. #define WCD939X_MBHC_GET_X1(x) (x & 0x3FFF)
  38. /* Z value compared in milliOhm */
  39. #define WCD939X_MBHC_IS_SECOND_RAMP_REQUIRED(z) false
  40. #define WCD939X_MBHC_ZDET_CONST (1071 * 1024)
  41. #define WCD939X_MBHC_MOISTURE_RREF R_24_KOHM
  42. #define OHMS_TO_MILLIOHMS 1000
  43. #define FLOAT_TO_FIXED_XTALK (1UL << 16)
  44. #define MAX_XTALK_ALPHA 255
  45. #define MIN_RL_EFF_MOHMS 1
  46. #define MAX_RL_EFF_MOHMS 900000
  47. #define HD2_CODE_BASE_VALUE 0x1D
  48. #define HD2_CODE_INV_RESOLUTION 4201025
  49. #define FLOAT_TO_FIXED_LINEARIZER (1UL << 12)
  50. #define MIN_TAP_OFFSET -1023
  51. #define MAX_TAP_OFFSET 1023
  52. #define MIN_TAP 0
  53. #define MAX_TAP 1023
  54. static struct wcd_mbhc_register
  55. wcd_mbhc_registers[WCD_MBHC_REG_FUNC_MAX] = {
  56. WCD_MBHC_REGISTER("WCD_MBHC_L_DET_EN",
  57. WCD939X_MBHC_MECH, 0x80, 7, 0),
  58. WCD_MBHC_REGISTER("WCD_MBHC_GND_DET_EN",
  59. WCD939X_MBHC_MECH, 0x40, 6, 0),
  60. WCD_MBHC_REGISTER("WCD_MBHC_MECH_DETECTION_TYPE",
  61. WCD939X_MBHC_MECH, 0x20, 5, 0),
  62. WCD_MBHC_REGISTER("WCD_MBHC_MIC_CLAMP_CTL",
  63. WCD939X_PLUG_DETECT_CTL, 0x30, 4, 0),
  64. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_DETECTION_TYPE",
  65. WCD939X_MBHC_ELECT, 0x08, 3, 0),
  66. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_CTRL",
  67. WCD939X_MECH_DET_CURRENT, 0x1F, 0, 0),
  68. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL",
  69. WCD939X_MBHC_MECH, 0x04, 2, 0),
  70. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PLUG_TYPE",
  71. WCD939X_MBHC_MECH, 0x10, 4, 0),
  72. WCD_MBHC_REGISTER("WCD_MBHC_GND_PLUG_TYPE",
  73. WCD939X_MBHC_MECH, 0x08, 3, 0),
  74. WCD_MBHC_REGISTER("WCD_MBHC_SW_HPH_LP_100K_TO_GND",
  75. WCD939X_MBHC_MECH, 0x01, 0, 0),
  76. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_SCHMT_ISRC",
  77. WCD939X_MBHC_ELECT, 0x06, 1, 0),
  78. WCD_MBHC_REGISTER("WCD_MBHC_FSM_EN",
  79. WCD939X_MBHC_ELECT, 0x80, 7, 0),
  80. WCD_MBHC_REGISTER("WCD_MBHC_INSREM_DBNC",
  81. WCD939X_PLUG_DETECT_CTL, 0x0F, 0, 0),
  82. WCD_MBHC_REGISTER("WCD_MBHC_BTN_DBNC",
  83. WCD939X_CTL_1, 0x03, 0, 0),
  84. WCD_MBHC_REGISTER("WCD_MBHC_HS_VREF",
  85. WCD939X_CTL_2, 0x03, 0, 0),
  86. WCD_MBHC_REGISTER("WCD_MBHC_HS_COMP_RESULT",
  87. WCD939X_MBHC_RESULT_3, 0x08, 3, 0),
  88. WCD_MBHC_REGISTER("WCD_MBHC_IN2P_CLAMP_STATE",
  89. WCD939X_MBHC_RESULT_3, 0x10, 4, 0),
  90. WCD_MBHC_REGISTER("WCD_MBHC_MIC_SCHMT_RESULT",
  91. WCD939X_MBHC_RESULT_3, 0x20, 5, 0),
  92. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_SCHMT_RESULT",
  93. WCD939X_MBHC_RESULT_3, 0x80, 7, 0),
  94. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_SCHMT_RESULT",
  95. WCD939X_MBHC_RESULT_3, 0x40, 6, 0),
  96. WCD_MBHC_REGISTER("WCD_MBHC_OCP_FSM_EN",
  97. WCD939X_HPH_OCP_CTL, 0x10, 4, 0),
  98. WCD_MBHC_REGISTER("WCD_MBHC_BTN_RESULT",
  99. WCD939X_MBHC_RESULT_3, 0x07, 0, 0),
  100. WCD_MBHC_REGISTER("WCD_MBHC_BTN_ISRC_CTL",
  101. WCD939X_MBHC_ELECT, 0x70, 4, 0),
  102. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_RESULT",
  103. WCD939X_MBHC_RESULT_3, 0xFF, 0, 0),
  104. WCD_MBHC_REGISTER("WCD_MBHC_MICB_CTRL",
  105. WCD939X_MICB2, 0xC0, 6, 0),
  106. WCD_MBHC_REGISTER("WCD_MBHC_HPH_CNP_WG_TIME",
  107. WCD939X_CNP_WG_TIME, 0xFF, 0, 0),
  108. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_PA_EN",
  109. WCD939X_HPH, 0x40, 6, 0),
  110. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PA_EN",
  111. WCD939X_HPH, 0x80, 7, 0),
  112. WCD_MBHC_REGISTER("WCD_MBHC_HPH_PA_EN",
  113. WCD939X_HPH, 0xC0, 6, 0),
  114. WCD_MBHC_REGISTER("WCD_MBHC_SWCH_LEVEL_REMOVE",
  115. WCD939X_MBHC_RESULT_3, 0x10, 4, 0),
  116. WCD_MBHC_REGISTER("WCD_MBHC_PULLDOWN_CTRL",
  117. 0, 0, 0, 0),
  118. WCD_MBHC_REGISTER("WCD_MBHC_ANC_DET_EN",
  119. WCD939X_CTL_BCS, 0x02, 1, 0),
  120. WCD_MBHC_REGISTER("WCD_MBHC_FSM_STATUS",
  121. WCD939X_FSM_STATUS, 0x01, 0, 0),
  122. WCD_MBHC_REGISTER("WCD_MBHC_MUX_CTL",
  123. WCD939X_CTL_2, 0x70, 4, 0),
  124. WCD_MBHC_REGISTER("WCD_MBHC_MOISTURE_STATUS",
  125. WCD939X_FSM_STATUS, 0x20, 5, 0),
  126. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_GND",
  127. WCD939X_PA_CTL2, 0x40, 6, 0),
  128. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_GND",
  129. WCD939X_PA_CTL2, 0x10, 4, 0),
  130. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_DET_EN",
  131. WCD939X_L_TEST, 0x01, 0, 0),
  132. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_DET_EN",
  133. WCD939X_R_TEST, 0x01, 0, 0),
  134. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_STATUS",
  135. WCD939X_INTR_STATUS_0, 0x80, 7, 0),
  136. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_STATUS",
  137. WCD939X_INTR_STATUS_0, 0x20, 5, 0),
  138. WCD_MBHC_REGISTER("WCD_MBHC_ADC_EN",
  139. WCD939X_CTL_1, 0x08, 3, 0),
  140. WCD_MBHC_REGISTER("WCD_MBHC_ADC_COMPLETE", WCD939X_FSM_STATUS,
  141. 0x40, 6, 0),
  142. WCD_MBHC_REGISTER("WCD_MBHC_ADC_TIMEOUT", WCD939X_FSM_STATUS,
  143. 0x80, 7, 0),
  144. WCD_MBHC_REGISTER("WCD_MBHC_ADC_RESULT", WCD939X_ADC_RESULT,
  145. 0xFF, 0, 0),
  146. WCD_MBHC_REGISTER("WCD_MBHC_MICB2_VOUT", WCD939X_MICB2, 0x3F, 0, 0),
  147. WCD_MBHC_REGISTER("WCD_MBHC_ADC_MODE",
  148. WCD939X_CTL_1, 0x10, 4, 0),
  149. WCD_MBHC_REGISTER("WCD_MBHC_DETECTION_DONE",
  150. WCD939X_CTL_1, 0x04, 2, 0),
  151. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_ISRC_EN",
  152. WCD939X_MBHC_ZDET, 0x02, 1, 0),
  153. };
  154. static const struct wcd_mbhc_intr intr_ids = {
  155. .mbhc_sw_intr = WCD939X_IRQ_MBHC_SW_DET,
  156. .mbhc_btn_press_intr = WCD939X_IRQ_MBHC_BUTTON_PRESS_DET,
  157. .mbhc_btn_release_intr = WCD939X_IRQ_MBHC_BUTTON_RELEASE_DET,
  158. .mbhc_hs_ins_intr = WCD939X_IRQ_MBHC_ELECT_INS_REM_LEG_DET,
  159. .mbhc_hs_rem_intr = WCD939X_IRQ_MBHC_ELECT_INS_REM_DET,
  160. .hph_left_ocp = WCD939X_IRQ_HPHL_OCP_INT,
  161. .hph_right_ocp = WCD939X_IRQ_HPHR_OCP_INT,
  162. };
  163. struct wcd939x_mbhc_zdet_param {
  164. u16 ldo_ctl;
  165. u16 noff;
  166. u16 nshift;
  167. u16 btn5;
  168. u16 btn6;
  169. u16 btn7;
  170. };
  171. static int wcd939x_mbhc_request_irq(struct snd_soc_component *component,
  172. int irq, irq_handler_t handler,
  173. const char *name, void *data)
  174. {
  175. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  176. return wcd_request_irq(&wcd939x->irq_info, irq, name, handler, data);
  177. }
  178. static void wcd939x_mbhc_irq_control(struct snd_soc_component *component,
  179. int irq, bool enable)
  180. {
  181. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  182. if (enable)
  183. wcd_enable_irq(&wcd939x->irq_info, irq);
  184. else
  185. wcd_disable_irq(&wcd939x->irq_info, irq);
  186. }
  187. static int wcd939x_mbhc_free_irq(struct snd_soc_component *component,
  188. int irq, void *data)
  189. {
  190. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  191. wcd_free_irq(&wcd939x->irq_info, irq, data);
  192. return 0;
  193. }
  194. static void wcd939x_mbhc_clk_setup(struct snd_soc_component *component,
  195. bool enable)
  196. {
  197. if (enable)
  198. snd_soc_component_update_bits(component, WCD939X_CTL_1,
  199. 0x80, 0x80);
  200. else
  201. snd_soc_component_update_bits(component, WCD939X_CTL_1,
  202. 0x80, 0x00);
  203. }
  204. static int wcd939x_mbhc_btn_to_num(struct snd_soc_component *component)
  205. {
  206. return snd_soc_component_read(component, WCD939X_MBHC_RESULT_3) & 0x7;
  207. }
  208. static void wcd939x_mbhc_mbhc_bias_control(struct snd_soc_component *component,
  209. bool enable)
  210. {
  211. if (enable)
  212. snd_soc_component_update_bits(component, WCD939X_MBHC_ELECT,
  213. 0x01, 0x01);
  214. else
  215. snd_soc_component_update_bits(component, WCD939X_MBHC_ELECT,
  216. 0x01, 0x00);
  217. }
  218. static void wcd939x_mbhc_program_btn_thr(struct snd_soc_component *component,
  219. s16 *btn_low, s16 *btn_high,
  220. int num_btn, bool is_micbias)
  221. {
  222. int i;
  223. int vth;
  224. if (num_btn > WCD_MBHC_DEF_BUTTONS) {
  225. dev_err_ratelimited(component->dev, "%s: invalid number of buttons: %d\n",
  226. __func__, num_btn);
  227. return;
  228. }
  229. for (i = 0; i < num_btn; i++) {
  230. vth = ((btn_high[i] * 2) / 25) & 0x3F;
  231. snd_soc_component_update_bits(component, WCD939X_MBHC_BTN0 + i,
  232. 0xFC, vth << 2);
  233. dev_dbg(component->dev, "%s: btn_high[%d]: %d, vth: %d\n",
  234. __func__, i, btn_high[i], vth);
  235. }
  236. }
  237. static bool wcd939x_mbhc_lock_sleep(struct wcd_mbhc *mbhc, bool lock)
  238. {
  239. struct snd_soc_component *component = mbhc->component;
  240. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  241. wcd939x->wakeup((void*)wcd939x, lock);
  242. return true;
  243. }
  244. static int wcd939x_mbhc_register_notifier(struct wcd_mbhc *mbhc,
  245. struct notifier_block *nblock,
  246. bool enable)
  247. {
  248. struct wcd939x_mbhc *wcd939x_mbhc;
  249. wcd939x_mbhc = container_of(mbhc, struct wcd939x_mbhc, wcd_mbhc);
  250. if (enable)
  251. return blocking_notifier_chain_register(&wcd939x_mbhc->notifier,
  252. nblock);
  253. else
  254. return blocking_notifier_chain_unregister(
  255. &wcd939x_mbhc->notifier, nblock);
  256. }
  257. static bool wcd939x_mbhc_micb_en_status(struct wcd_mbhc *mbhc, int micb_num)
  258. {
  259. u8 val = 0;
  260. if (micb_num == MIC_BIAS_2) {
  261. val = ((snd_soc_component_read(mbhc->component,
  262. WCD939X_MICB2) & 0xC0)
  263. >> 6);
  264. if (val == 0x01)
  265. return true;
  266. }
  267. return false;
  268. }
  269. static bool wcd939x_mbhc_hph_pa_on_status(struct snd_soc_component *component)
  270. {
  271. return (snd_soc_component_read(component, WCD939X_HPH) & 0xC0) ?
  272. true : false;
  273. }
  274. static void wcd939x_mbhc_hph_l_pull_up_control(
  275. struct snd_soc_component *component,
  276. int pull_up_cur)
  277. {
  278. /* Default pull up current to 2uA */
  279. if (pull_up_cur > HS_PULLUP_I_OFF || pull_up_cur < HS_PULLUP_I_3P0_UA ||
  280. pull_up_cur == HS_PULLUP_I_DEFAULT)
  281. pull_up_cur = HS_PULLUP_I_2P0_UA;
  282. dev_dbg(component->dev, "%s: HS pull up current:%d\n",
  283. __func__, pull_up_cur);
  284. snd_soc_component_update_bits(component,
  285. WCD939X_MECH_DET_CURRENT,
  286. 0x1F, pull_up_cur);
  287. }
  288. static int wcd939x_mbhc_request_micbias(struct snd_soc_component *component,
  289. int micb_num, int req)
  290. {
  291. int ret = 0;
  292. ret = wcd939x_micbias_control(component, micb_num, req, false);
  293. return ret;
  294. }
  295. static void wcd939x_mbhc_micb_ramp_control(struct snd_soc_component *component,
  296. bool enable)
  297. {
  298. if (enable) {
  299. snd_soc_component_update_bits(component, WCD939X_MICB2_RAMP,
  300. 0x1C, 0x0C);
  301. snd_soc_component_update_bits(component, WCD939X_MICB2_RAMP,
  302. 0x80, 0x80);
  303. } else {
  304. snd_soc_component_update_bits(component, WCD939X_MICB2_RAMP,
  305. 0x80, 0x00);
  306. snd_soc_component_update_bits(component, WCD939X_MICB2_RAMP,
  307. 0x1C, 0x00);
  308. }
  309. }
  310. static struct firmware_cal *wcd939x_get_hwdep_fw_cal(struct wcd_mbhc *mbhc,
  311. enum wcd_cal_type type)
  312. {
  313. struct wcd939x_mbhc *wcd939x_mbhc;
  314. struct firmware_cal *hwdep_cal;
  315. struct snd_soc_component *component = mbhc->component;
  316. wcd939x_mbhc = container_of(mbhc, struct wcd939x_mbhc, wcd_mbhc);
  317. if (!component) {
  318. pr_err_ratelimited("%s: NULL component pointer\n", __func__);
  319. return NULL;
  320. }
  321. hwdep_cal = wcdcal_get_fw_cal(wcd939x_mbhc->fw_data, type);
  322. if (!hwdep_cal)
  323. dev_err_ratelimited(component->dev, "%s: cal not sent by %d\n",
  324. __func__, type);
  325. return hwdep_cal;
  326. }
  327. static int wcd939x_mbhc_micb_ctrl_threshold_mic(
  328. struct snd_soc_component *component,
  329. int micb_num, bool req_en)
  330. {
  331. struct wcd939x_pdata *pdata = dev_get_platdata(component->dev);
  332. int rc, micb_mv;
  333. if (micb_num != MIC_BIAS_2)
  334. return -EINVAL;
  335. /*
  336. * If device tree micbias level is already above the minimum
  337. * voltage needed to detect threshold microphone, then do
  338. * not change the micbias, just return.
  339. */
  340. if (pdata->micbias.micb2_mv >= WCD_MBHC_THR_HS_MICB_MV)
  341. return 0;
  342. micb_mv = req_en ? WCD_MBHC_THR_HS_MICB_MV : pdata->micbias.micb2_mv;
  343. rc = wcd939x_mbhc_micb_adjust_voltage(component, micb_mv, MIC_BIAS_2);
  344. return rc;
  345. }
  346. static inline void wcd939x_mbhc_get_result_params(struct wcd939x_priv *wcd939x,
  347. s16 *d1_a, u16 noff,
  348. int32_t *zdet)
  349. {
  350. int i;
  351. int val, val1;
  352. s16 c1;
  353. s32 x1, d1;
  354. int32_t denom;
  355. int minCode_param[] = {
  356. 3277, 1639, 820, 410, 205, 103, 52, 26
  357. };
  358. regmap_update_bits(wcd939x->regmap, WCD939X_MBHC_ZDET, 0x20, 0x20);
  359. for (i = 0; i < WCD939X_ZDET_NUM_MEASUREMENTS; i++) {
  360. regmap_read(wcd939x->regmap, WCD939X_MBHC_RESULT_2, &val);
  361. if (val & 0x80)
  362. break;
  363. }
  364. val = val << 0x8;
  365. regmap_read(wcd939x->regmap, WCD939X_MBHC_RESULT_1, &val1);
  366. val |= val1;
  367. regmap_update_bits(wcd939x->regmap, WCD939X_MBHC_ZDET, 0x20, 0x00);
  368. x1 = WCD939X_MBHC_GET_X1(val);
  369. c1 = WCD939X_MBHC_GET_C1(val);
  370. /* If ramp is not complete, give additional 5ms */
  371. if ((c1 < 2) && x1)
  372. usleep_range(5000, 5050);
  373. if (!c1 || !x1) {
  374. dev_dbg(wcd939x->dev,
  375. "%s: Impedance detect ramp error, c1=%d, x1=0x%x\n",
  376. __func__, c1, x1);
  377. goto ramp_down;
  378. }
  379. d1 = d1_a[c1];
  380. denom = (x1 * d1) - (1 << (14 - noff));
  381. if (denom > 0)
  382. *zdet = (WCD939X_MBHC_ZDET_CONST * 1000) / denom;
  383. else if (x1 < minCode_param[noff])
  384. *zdet = WCD939X_ZDET_FLOATING_IMPEDANCE;
  385. dev_dbg(wcd939x->dev, "%s: d1=%d, c1=%d, x1=0x%x, z_val=%d(milliOhm)\n",
  386. __func__, d1, c1, x1, *zdet);
  387. ramp_down:
  388. i = 0;
  389. while (x1) {
  390. regmap_read(wcd939x->regmap,
  391. WCD939X_MBHC_RESULT_1, &val);
  392. regmap_read(wcd939x->regmap,
  393. WCD939X_MBHC_RESULT_2, &val1);
  394. val = val << 0x08;
  395. val |= val1;
  396. x1 = WCD939X_MBHC_GET_X1(val);
  397. i++;
  398. if (i == WCD939X_ZDET_NUM_MEASUREMENTS)
  399. break;
  400. }
  401. }
  402. static void wcd939x_mbhc_zdet_ramp(struct snd_soc_component *component,
  403. struct wcd939x_mbhc_zdet_param *zdet_param,
  404. int32_t *zl, int32_t *zr, s16 *d1_a)
  405. {
  406. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  407. int32_t zdet = 0;
  408. snd_soc_component_update_bits(component, WCD939X_ZDET_ANA_CTL, 0xF0,
  409. 0x80 | (zdet_param->ldo_ctl << 4));
  410. snd_soc_component_update_bits(component, WCD939X_MBHC_BTN5, 0xFC,
  411. zdet_param->btn5);
  412. snd_soc_component_update_bits(component, WCD939X_MBHC_BTN6, 0xFC,
  413. zdet_param->btn6);
  414. snd_soc_component_update_bits(component, WCD939X_MBHC_BTN7, 0xFC,
  415. zdet_param->btn7);
  416. snd_soc_component_update_bits(component, WCD939X_ZDET_ANA_CTL,
  417. 0x0F, zdet_param->noff);
  418. snd_soc_component_update_bits(component, WCD939X_ZDET_RAMP_CTL,
  419. 0x0F, zdet_param->nshift);
  420. snd_soc_component_update_bits(component, WCD939X_ZDET_RAMP_CTL,
  421. 0x70, 0x60); /*acc1_min_63 */
  422. if (!zl)
  423. goto z_right;
  424. /* Start impedance measurement for HPH_L */
  425. regmap_update_bits(wcd939x->regmap,
  426. WCD939X_MBHC_ZDET, 0x80, 0x80);
  427. dev_dbg(wcd939x->dev, "%s: ramp for HPH_L, noff = %d\n",
  428. __func__, zdet_param->noff);
  429. wcd939x_mbhc_get_result_params(wcd939x, d1_a, zdet_param->noff, &zdet);
  430. regmap_update_bits(wcd939x->regmap,
  431. WCD939X_MBHC_ZDET, 0x80, 0x00);
  432. *zl = zdet;
  433. z_right:
  434. if (!zr)
  435. return;
  436. /* Start impedance measurement for HPH_R */
  437. regmap_update_bits(wcd939x->regmap,
  438. WCD939X_MBHC_ZDET, 0x40, 0x40);
  439. dev_dbg(wcd939x->dev, "%s: ramp for HPH_R, noff = %d\n",
  440. __func__, zdet_param->noff);
  441. wcd939x_mbhc_get_result_params(wcd939x, d1_a, zdet_param->noff, &zdet);
  442. regmap_update_bits(wcd939x->regmap,
  443. WCD939X_MBHC_ZDET, 0x40, 0x00);
  444. *zr = zdet;
  445. }
  446. static inline void wcd939x_wcd_mbhc_qfuse_cal(
  447. struct snd_soc_component *component,
  448. int32_t *z_val, int flag_l_r)
  449. {
  450. s16 q1;
  451. int q1_cal;
  452. q1 = snd_soc_component_read(component,
  453. WCD939X_EFUSE_REG_21 + flag_l_r);
  454. if (q1 & 0x80)
  455. q1_cal = (10000 - ((q1 & 0x7F) * 10));
  456. else
  457. q1_cal = (10000 + (q1 * 10));
  458. if (q1_cal > 0)
  459. *z_val = ((*z_val) * 10000) / q1_cal;
  460. }
  461. static void update_hd2_codes(struct regmap *regmap, u32 r_gnd_res_tot_mohms, u32 r_load_eff)
  462. {
  463. u64 hd2_delta = 0;
  464. if (!regmap)
  465. return;
  466. hd2_delta = (HD2_CODE_INV_RESOLUTION * (u64) r_gnd_res_tot_mohms +
  467. FLOAT_TO_FIXED_XTALK * (u64) ((r_gnd_res_tot_mohms + r_load_eff) / 2)) /
  468. (FLOAT_TO_FIXED_XTALK * (u64) (r_gnd_res_tot_mohms + r_load_eff));
  469. if (hd2_delta >= HD2_CODE_BASE_VALUE) {
  470. regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_L, 0x1F, 0x00);
  471. regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_R, 0x1F, 0x00);
  472. } else {
  473. regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_L, 0x1F,
  474. HD2_CODE_BASE_VALUE - hd2_delta);
  475. regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_R, 0x1F,
  476. HD2_CODE_BASE_VALUE - hd2_delta);
  477. }
  478. }
  479. static u8 get_xtalk_scale(u32 gain)
  480. {
  481. u8 i;
  482. int target, residue;
  483. if (gain == 0)
  484. return MAX_XTALK_SCALE;
  485. target = FLOAT_TO_FIXED_XTALK / ((int) gain);
  486. residue = target;
  487. for (i = 0; i <= MAX_XTALK_SCALE; i++) {
  488. residue = target - (1 << ((int)((u32) i)));
  489. if (residue < 0)
  490. return i;
  491. }
  492. return MAX_XTALK_SCALE;
  493. }
  494. static u8 get_xtalk_alpha(u32 gain, u8 scale)
  495. {
  496. u32 two_exp_scale, round_offset, alpha;
  497. if (gain == 0)
  498. return MIN_XTALK_ALPHA;
  499. two_exp_scale = 1 << ((u32) scale);
  500. round_offset = FLOAT_TO_FIXED_XTALK / 2;
  501. alpha = (((gain * two_exp_scale - FLOAT_TO_FIXED_XTALK) * 255) + round_offset)
  502. / FLOAT_TO_FIXED_XTALK;
  503. return (alpha <= MAX_XTALK_ALPHA) ? ((u8) alpha) : MAX_XTALK_ALPHA;
  504. }
  505. static u32 get_v_common_gnd_factor(u32 r_gnd_res_tot_mohms, u32 r_load_eff_mohms,
  506. u32 r_aud_res_tot_mohms)
  507. {
  508. /* Proof 1: The numerator does not overflow.
  509. * r_gnd_res_tot_mohms = r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms =
  510. * r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_route1_mohms +
  511. * r_gnd_par_route2_mohms
  512. *
  513. * r_gnd_int_fet_mohms, r_gnd_ext_fet_mohms, r_gnd_par_route{1,2}_mohms are all less
  514. * than MAX_USBCSS_HS_IMPEDANCE_MOHMS
  515. * -->
  516. * FLOAT_TO_FIXED_XTALK * r_gnd_res_tot_mohms <=
  517. * FLOAT_TO_FIXED_XTALK * 4 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
  518. * (1 << 16) * 4 * 20,000 = 65,536 * 80,000 = 3,932,160,000 <= 2^32 - 1 =
  519. * 4,294,967,295 = U32_MAX
  520. *
  521. * Proof 2: The denominator is greater than 0.
  522. * r_load_eff_mohms >= MIN_RL_EFF_MOHMS = 1 > 0
  523. * -->
  524. * r_load_eff_mohms + r_aud_res_tot_mohms + r_gnd_res_tot_mohms > 0
  525. *
  526. * Proof 3: The deonominator does not overflow.
  527. * r_load_eff_mohms <= MAX_RL_EFF_MOHMS
  528. * r_aud_res_tot_mohms and r_gnd_res_tot_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS
  529. * -->
  530. * r_load_eff_mohms + r_aud_res_tot_mohms + r_gnd_res_tot_mohms <=
  531. * MAX_RL_EFF_MOHMS + 2 * MAX_USBCSS_HS_IMPEDANCE_MOHMS = 900,000 + 2 * 20,000 = 940,000
  532. * <= U32_MAX = 2^32 - 1 = 4,294,967,295
  533. */
  534. return FLOAT_TO_FIXED_XTALK * r_gnd_res_tot_mohms /
  535. (r_load_eff_mohms + r_aud_res_tot_mohms + r_gnd_res_tot_mohms);
  536. }
  537. static u32 get_v_feedback_tap_factor_digital(u32 r_gnd_int_fet_mohms, u32 r_gnd_par_route1_mohms,
  538. u32 r_load_eff_mohms, u32 r_gnd_res_tot_mohms,
  539. u32 r_aud_res_tot_mohms)
  540. {
  541. /* Proof 4: The numerator does not overflow.
  542. * r_gnd_int_fet_mohms and r_gnd_par_route1_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS
  543. * -->
  544. * FLOAT_TO_FIXED_XTALK * (r_gnd_int_fet_mohms + r_gnd_par_route1_mohms) <=
  545. * FLOAT_TO_FIXED_XTALK * 2 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
  546. * (1 << 16) * 2 * 20,000 = 65,536 * 40,000 = 2,621,440,000 <= 2^32 - 1 =
  547. * 4,294,967,295 = U32_MAX
  548. *
  549. * The denominator is greater than 0: See Proof 2
  550. * The deonominator does not overflow: See Proof 3
  551. */
  552. return FLOAT_TO_FIXED_XTALK * (r_gnd_int_fet_mohms + r_gnd_par_route1_mohms) /
  553. (r_load_eff_mohms + r_gnd_res_tot_mohms + r_aud_res_tot_mohms);
  554. }
  555. static u32 get_v_feedback_tap_factor_analog(u32 r_gnd_par_route2_mohms, u32 r_load_eff_mohms,
  556. u32 r_gnd_res_tot_mohms, u32 r_aud_res_tot_mohms)
  557. {
  558. /* Proof 5: The numerator does not overflow.
  559. * r_gnd_res_tot_mohms = r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms =
  560. * r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_route1_mohms +
  561. * r_gnd_par_route2_mohms
  562. *
  563. * r_gnd_res_tot_mohms - r_gnd_par_route2_mohms =
  564. * r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_route1_mohms
  565. *
  566. * r_gnd_int_fet_mohms, r_gnd_ext_fet_mohms, r_gnd_par_route1_mohms
  567. * <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
  568. * -->
  569. * FLOAT_TO_FIXED_XTALK * (r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms +
  570. * r_gnd_par_route1_mohms)
  571. * <= FLOAT_TO_FIXED_XTALK * 3 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
  572. * (1 << 16) * 3 * 20,000 = 65,536 * 60,000 = 3,932,160,000 <= 2^32 - 1 =
  573. * 4,294,967,295 = U32_MAX
  574. *
  575. * The denominator is greater than 0: See Proof 2
  576. * The deonominator does not overflow: See Proof 3
  577. */
  578. return FLOAT_TO_FIXED_XTALK * (r_gnd_res_tot_mohms - r_gnd_par_route2_mohms) /
  579. (r_load_eff_mohms + r_gnd_res_tot_mohms + r_aud_res_tot_mohms);
  580. }
  581. static u32 get_xtalk_gain(u32 v_common_gnd_factor, u32 v_feedback_tap_factor)
  582. {
  583. return v_common_gnd_factor - v_feedback_tap_factor;
  584. }
  585. static void update_xtalk_scale_and_alpha(struct wcd939x_pdata *pdata, struct regmap *regmap)
  586. {
  587. u32 r_gnd_res_tot_mohms = 0, r_gnd_int_fet_mohms = 0, v_common_gnd_factor = 0;
  588. u32 v_feedback_tap_factor = 0, xtalk_gain = 0;
  589. if (!pdata || pdata->usbcss_hs.xtalk_config == XTALK_NONE)
  590. return;
  591. /* Orientation-dependent ground impedance parameters */
  592. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  593. if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
  594. r_gnd_res_tot_mohms = pdata->usbcss_hs.r_gnd_sbu2_res_tot_mohms;
  595. r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu2_int_fet_mohms;
  596. } else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
  597. r_gnd_res_tot_mohms = pdata->usbcss_hs.r_gnd_sbu1_res_tot_mohms;
  598. r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu1_int_fet_mohms;
  599. } else {
  600. pdata->usbcss_hs.scale_l = MAX_XTALK_SCALE;
  601. pdata->usbcss_hs.alpha_l = MIN_XTALK_ALPHA;
  602. pdata->usbcss_hs.scale_r = MAX_XTALK_SCALE;
  603. pdata->usbcss_hs.alpha_r = MIN_XTALK_ALPHA;
  604. return;
  605. }
  606. #endif
  607. /* Recall assumptions about L and R channel impedance parameters being equivalent */
  608. /* Xtalk gain calculation */
  609. v_common_gnd_factor = get_v_common_gnd_factor(r_gnd_res_tot_mohms,
  610. pdata->usbcss_hs.r_load_eff_l_mohms,
  611. pdata->usbcss_hs.r_aud_res_tot_l_mohms);
  612. if (pdata->usbcss_hs.xtalk_config == XTALK_ANALOG) {
  613. v_feedback_tap_factor = get_v_feedback_tap_factor_analog(
  614. pdata->usbcss_hs.r_gnd_par_route2_mohms,
  615. pdata->usbcss_hs.r_load_eff_l_mohms,
  616. r_gnd_res_tot_mohms,
  617. pdata->usbcss_hs.r_aud_res_tot_l_mohms);
  618. /* Update HD2 codes for analog xtalk */
  619. update_hd2_codes(regmap, r_gnd_res_tot_mohms, pdata->usbcss_hs.r_load_eff_l_mohms);
  620. } else {
  621. v_feedback_tap_factor = get_v_feedback_tap_factor_digital(
  622. r_gnd_int_fet_mohms,
  623. pdata->usbcss_hs.r_gnd_par_route1_mohms,
  624. pdata->usbcss_hs.r_load_eff_l_mohms,
  625. r_gnd_res_tot_mohms,
  626. pdata->usbcss_hs.r_aud_res_tot_l_mohms);
  627. }
  628. xtalk_gain = get_xtalk_gain(v_common_gnd_factor, v_feedback_tap_factor);
  629. /* Store scale and alpha values */
  630. pdata->usbcss_hs.scale_l = get_xtalk_scale(xtalk_gain);
  631. pdata->usbcss_hs.alpha_l = get_xtalk_alpha(xtalk_gain, pdata->usbcss_hs.scale_l);
  632. pdata->usbcss_hs.scale_r = pdata->usbcss_hs.scale_l;
  633. pdata->usbcss_hs.alpha_r = pdata->usbcss_hs.alpha_l;
  634. }
  635. static void update_ext_fet_res(struct wcd939x_pdata *pdata, u32 r_gnd_ext_fet_mohms)
  636. {
  637. if (!pdata)
  638. return;
  639. pdata->usbcss_hs.r_gnd_ext_fet_mohms = (r_gnd_ext_fet_mohms > MAX_USBCSS_HS_IMPEDANCE_MOHMS)
  640. ? MAX_USBCSS_HS_IMPEDANCE_MOHMS
  641. : r_gnd_ext_fet_mohms;
  642. pdata->usbcss_hs.r_aud_ext_fet_l_mohms = pdata->usbcss_hs.r_gnd_ext_fet_mohms;
  643. pdata->usbcss_hs.r_aud_ext_fet_r_mohms = pdata->usbcss_hs.r_gnd_ext_fet_mohms;
  644. pdata->usbcss_hs.r_gnd_sbu1_res_tot_mohms = get_r_gnd_res_tot_mohms(
  645. pdata->usbcss_hs.r_gnd_sbu1_int_fet_mohms,
  646. pdata->usbcss_hs.r_gnd_ext_fet_mohms,
  647. pdata->usbcss_hs.r_gnd_par_tot_mohms);
  648. pdata->usbcss_hs.r_gnd_sbu2_res_tot_mohms = get_r_gnd_res_tot_mohms(
  649. pdata->usbcss_hs.r_gnd_sbu2_int_fet_mohms,
  650. pdata->usbcss_hs.r_gnd_ext_fet_mohms,
  651. pdata->usbcss_hs.r_gnd_par_tot_mohms);
  652. pdata->usbcss_hs.r_aud_res_tot_l_mohms = get_r_aud_res_tot_mohms(
  653. pdata->usbcss_hs.r_aud_int_fet_l_mohms,
  654. pdata->usbcss_hs.r_aud_ext_fet_l_mohms);
  655. pdata->usbcss_hs.r_aud_res_tot_r_mohms = get_r_aud_res_tot_mohms(
  656. pdata->usbcss_hs.r_aud_int_fet_r_mohms,
  657. pdata->usbcss_hs.r_aud_ext_fet_r_mohms);
  658. }
  659. static void get_linearizer_taps(struct wcd939x_pdata *pdata, u32 *aud_tap, u32 *gnd_tap)
  660. {
  661. u32 r_gnd_res_tot_mohms = 0, r_gnd_int_fet_mohms = 0, v_aud1 = 0, v_aud2 = 0;
  662. u32 v_gnd_denom = 0, v_gnd1 = 0, v_gnd2 = 0, aud_denom = 0, gnd_denom = 0;
  663. if (!pdata)
  664. goto err_data;
  665. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  666. /* Orientation-dependent ground impedance parameters */
  667. if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
  668. r_gnd_res_tot_mohms = pdata->usbcss_hs.r_gnd_sbu2_res_tot_mohms;
  669. r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu2_int_fet_mohms;
  670. } else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
  671. r_gnd_res_tot_mohms = pdata->usbcss_hs.r_gnd_sbu1_res_tot_mohms;
  672. r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu1_int_fet_mohms;
  673. } else {
  674. goto err_data;
  675. }
  676. #endif
  677. /* Proof 6: Neither aud_denom nor gnd_denom is 0 and neither overflows.
  678. * MIN_K_TIMES_100 = -50 <= MAX_K_TIMES_100 <= 10,000 = k_aud_times_100
  679. * -->
  680. * 0 < 410 = 0.1 * 4,096 = 0.1 * FLOAT_TO_FIXED_LINEARIZER < {aud,gnd}_denom <
  681. * 101 * FLOAT_TO_FIXED_LINEARIZER =
  682. * 101 * (1 << 12) < 413,696 <= 4,294,967,295 = U32_MAX
  683. */
  684. aud_denom = (u32) (FLOAT_TO_FIXED_LINEARIZER +
  685. (FLOAT_TO_FIXED_LINEARIZER * pdata->usbcss_hs.k_aud_times_100 / 100));
  686. gnd_denom = (u32) (FLOAT_TO_FIXED_LINEARIZER +
  687. (FLOAT_TO_FIXED_LINEARIZER * pdata->usbcss_hs.k_gnd_times_100 / 100));
  688. /* Proof 7: v_aud2 does not overflow.
  689. * MIN_RL_EFF_MOHMS = 1 = <= pdata->usbcss_hs.r_load_eff_l_mohms <= MAX_RL_EFF_MOHMS =
  690. * 900,000
  691. *
  692. * pdata->usbcss_hs.r_gnd_par_tot_mohms = r_gnd_par_route1_mohms + r_gnd_par_route2_mohms
  693. * <= 2 * MAX_USBCSS_HS_IMPEDANCE_MOHMS = 4,0000
  694. *
  695. * r_gnd_int_fet_mohms, pdata->usbcss_hs.r_gnd_ext_fet_mohms, r_gnd_par_route1_mohms,
  696. * r_gnd_par_route2_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
  697. * -->
  698. * 1 <= v_aud2 <= MAX_RL_EFF_MOHMS + 4 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
  699. * 900,000 + 4 * 20,000 = 980,000 <= 4,294,967,295 = U32_MAX
  700. */
  701. v_aud2 = pdata->usbcss_hs.r_load_eff_l_mohms - pdata->usbcss_hs.r3 + r_gnd_int_fet_mohms +
  702. pdata->usbcss_hs.r_gnd_ext_fet_mohms + pdata->usbcss_hs.r_gnd_par_tot_mohms;
  703. /* Proof 8: v_aud1 does not overflow.
  704. * pdata->usbcss_hs.r_aud_ext_fet_l_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
  705. * From Proof 7,
  706. * 1 <= v_aud2 <= MAX_RL_EFF_MOHMS + 4 * MAX_USBCSS_HS_IMPEDANCE_MOHMS <= S32_MAX
  707. * -->
  708. * 1 <= v_aud1 <= MAX_RL_EFF_MOHMS + 5 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
  709. * 900,000 + 5 * 20,000 = 1,000,000 <= 2,147,483,647 = S32_MAX
  710. */
  711. v_aud1 = v_aud2 + pdata->usbcss_hs.r_aud_ext_fet_l_mohms;
  712. /* Proof 9: The numerator of v_aud1 does not overflow.
  713. * From Proof 8, v_aud1 was less than or equal to 1,000,000
  714. * Thus, the new v_aud1 numerator is less than or equal to
  715. * FLOAT_TO_FIXED_LINEARIZER * 1,000,000 =
  716. * 4,096 * 1,000,000 = 4,096,000,000 <= 4,294,967,295 = U32_MAX
  717. *
  718. * Proof 10: The denominator of v_aud1 is not 0.
  719. * From Proof 8, v_aud1 was greater than or equal to 1 > 0
  720. *
  721. * Proof 11: The denominator does not overflow.
  722. * From Proof 8, v_aud1 was less than or equal to 1,000,000
  723. * Thus, the new v_aud1 denominator is less than or equal to
  724. * 1,000,000 + pdata->usbcss_hs.r_aud_int_fet_l_mohms = 1,000,000 + 20,000 = 1,020,000 <=
  725. * 4,294,967,295 = U32_MAX
  726. */
  727. v_aud1 = FLOAT_TO_FIXED_LINEARIZER * v_aud1 /
  728. (v_aud1 + pdata->usbcss_hs.r_aud_int_fet_l_mohms);
  729. /* Proof 12: The numerator of v_aud2 does not overflow.
  730. * From Proof 7, v_aud2 was less than or equal to 980,000
  731. * Thus, the new v_aud2 numerator is less than or equal to
  732. * FLOAT_TO_FIXED_LINEARIZER * 980,000 =
  733. * 4,096 * 980,000 = 4,014,080,000 <= 4,294,967,295 = U32_MAX
  734. *
  735. * Proof 13: The denominator of v_aud2 is not 0.
  736. * From Proof 7, v_aud2 was greater than or equal to 1 > 0
  737. *
  738. * Proof 14: The denominator does not overflow.
  739. * From Proof 7, v_aud2 was less than or equal to 980,000
  740. * Thus, the new v_aud2 denominator is less than or equal to
  741. * 980,000 + pdata->usbcss_hs.r_aud_int_fet_l_mohms pdata->usbcss_hs.r_aud_int_fet_l_mohms =
  742. * 980,000 + 20,000 + + 20,000 = 1,020,000 <= 4,294,967,295 = U32_MAX
  743. */
  744. v_aud2 = FLOAT_TO_FIXED_LINEARIZER * v_aud2 /
  745. (v_aud2 + pdata->usbcss_hs.r_aud_ext_fet_l_mohms +
  746. pdata->usbcss_hs.r_aud_int_fet_l_mohms);
  747. /* Proof 15: The numerator of aud_tap does not overflow.
  748. * Looking at the formula for v_aud1 from Proofs 9 to 11, the greatest value of v_aud1 is
  749. * FLOAT_TO_FIXED_LINEARIZER = 4,096
  750. * Looking at the formula for v_aud2 from Proofs 12 to 14, the greatest value of v_aud2 is
  751. * FLOAT_TO_FIXED_LINEARIZER = 4,096
  752. * From Proof 6, aud_denom <= 413,696
  753. * Thus, the numerator <= 1,000 * 4,096 + 10 * 10,000 * 4,096 + 413,696 / 2 =
  754. * 4,096,000 + 409,600,000 + 206,848 = 413,902,848 <= 4,294,967,295 = U32_MAX
  755. *
  756. * Proof 16: The denominator of aud_tap is not 0.
  757. * From Proof 6, aud_denom > 410 > 0
  758. *
  759. * Proof 17: The denominator of aud_tap does not overflow
  760. * From Proof 6, aud_denom <= 413,696 <= 4,294,967,295 = U32_MAX
  761. *
  762. * Proof 18: The result of aud_tap does not overflow.
  763. * From Proof 15, the numerator <= 413,902,848 and from Proof 16, the denominator > 410
  764. * Thus, the divsion will be at most 1,009,519.
  765. * pdata->usbcss_hs.aud_tap_offset <= MAX_TAP_OFFSET = 1,023
  766. * The sum will thus be bounded by 1,009,519 + 1,023 = 1,010,542 <= 2,147,483,647 = S32_MAX
  767. * Note: aud_tap won't underflow either since pdata->usbcss_hs.aud_tap_offset >= -1,023
  768. */
  769. *aud_tap = (u32) ((s32) ((1000 * v_aud1 + 10 * pdata->usbcss_hs.k_aud_times_100 * v_aud2
  770. + aud_denom / 2) / aud_denom) + pdata->usbcss_hs.aud_tap_offset);
  771. if (*aud_tap > MAX_TAP)
  772. *aud_tap = MAX_TAP;
  773. else if (*aud_tap < MIN_TAP)
  774. *aud_tap = MIN_TAP;
  775. /* Proof 19: v_gnd_denom does not overflow.
  776. * r_gnd_res_tot_mohms = r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms
  777. *
  778. * r_gnd_int_fet_mohms, r_gnd_ext_fet_mohms, r_gnd_par_tot_mohms,
  779. * pdata->usbcss_hs.r_aud_ext_fet_l_mohms, pdata->usbcss_hs.r_aud_int_fet_l_mohms are all
  780. * <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
  781. *
  782. * pdata->usbcss_hs.r_load_eff_l_mohms <= MAX_RL_EFF_MOHMS = 900,000
  783. *
  784. * --> v_gnd_denom <= 3 * 20,000 + 900,000 + 2 * 20,000 = 60,000 + 900,000 + 40,000 =
  785. * 1,000,000 <= 4,294,967,295 = U32_MAX
  786. *
  787. * Proof 20: v_gnd_denom is not 0.
  788. * pdata->usbcss_hs.r_load_eff_l_mohms >= MIN_RL_EFF_MOHMS = 1
  789. * --> v_gnd_denom >= 1 > 0
  790. */
  791. v_gnd_denom = (r_gnd_res_tot_mohms + pdata->usbcss_hs.r_load_eff_l_mohms -
  792. pdata->usbcss_hs.r3 + pdata->usbcss_hs.r_aud_ext_fet_l_mohms +
  793. pdata->usbcss_hs.r_aud_int_fet_l_mohms);
  794. /* Proof 21: v_gnd1 numerator does not overflow.
  795. * r_gnd_int_fet_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
  796. * --> v_gnd1 numerator <= 4,096 * 20,000 = 81,920,000 <= 4,294,967,295 = U32_MAX
  797. *
  798. * v_gnd1 denominator is not 0: See Proof 20
  799. * v_gnd1 denominator does not overflow: See Proof 19
  800. */
  801. v_gnd1 = FLOAT_TO_FIXED_LINEARIZER * r_gnd_int_fet_mohms / v_gnd_denom;
  802. /* Proof 22: v_gnd2 numerator does not overflow.
  803. * r_gnd_int_fet_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
  804. * pdata->usbcss_hs.r_load_eff_l_mohms <= MAX_RL_EFF_MOHMS = 900,000
  805. * --> v_gnd2 numerator <= 4,096 * (20,000 + 900,000) = 4,096 * 920,000 = 3,768,320,000
  806. * <= 4,294,967,295 = U32_MAX
  807. *
  808. * v_gnd2 denominator is not 0: See Proof 20
  809. * v_gnd2 denominator does not overflow: See Proof 19
  810. */
  811. v_gnd2 = FLOAT_TO_FIXED_LINEARIZER * (r_gnd_int_fet_mohms +
  812. pdata->usbcss_hs.r_gnd_ext_fet_mohms) / v_gnd_denom;
  813. /* Proof 23: The numerator of gnd_tap does not overflow.
  814. * Looking at the formula for v_gnd1 from Proof 21, and considering that
  815. * r_gnd_res_tot_mohms = r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms,
  816. * the greatest value of v_gnd1 is FLOAT_TO_FIXED_LINEARIZER = 4,096.
  817. * Looking at the formula for v_aud2 from Proof 22 and again at the definintion of
  818. * r_gnd_res_tot_mohms, the greatest value of v_gnd2 is FLOAT_TO_FIXED_LINEARIZER = 4,096
  819. * From Proof 6, gnd_denom <= 413,696
  820. * Thus, the numerator <= 1,000 * 4,096 + 10 * 10,000 * 4,096 + 413,696 / 2 =
  821. * 4,096,000 + 409,600,000 + 206,848 = 413,902,848 <= 4,294,967,295 = U32_MAX
  822. *
  823. * Proof 24: The denominator of gnd_tap is not 0.
  824. * From Proof 6, gnd_denom > 410 > 0
  825. *
  826. * Proof 25: The denominator of gnd_tap does not overflow
  827. * From Proof 6, gnd_denom <= 413,696 <= 4,294,967,295 = U32_MAX
  828. *
  829. * Proof 26: The result of aud_tap does not overflow.
  830. * From Proof 15, the numerator <= 413,902,848 and from Proof 16, the denominator > 410
  831. * Thus, the divsion will be at most 1,009,519.
  832. * pdata->usbcss_hs.aud_tap_offset <= MAX_TAP_OFFSET = 1,023
  833. * The sum will thus be bounded by 1,009,519 + 1,023 = 1,010,542 <= 2,147,483,647 = S32_MAX
  834. * Note: gnd_tap won't underflow either since pdata->usbcss_hs.aud_tap_offset >= -1,023
  835. */
  836. *gnd_tap = (u32) ((s32) ((1000 * v_gnd1 + 10 * pdata->usbcss_hs.k_gnd_times_100 * v_gnd2
  837. + gnd_denom / 2) / gnd_denom) + pdata->usbcss_hs.gnd_tap_offset);
  838. if (*gnd_tap > MAX_TAP)
  839. *gnd_tap = MAX_TAP;
  840. else if (*gnd_tap < MIN_TAP)
  841. *gnd_tap = MIN_TAP;
  842. return;
  843. err_data:
  844. *aud_tap = 0;
  845. *gnd_tap = 0;
  846. }
  847. static void wcd939x_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl, uint32_t *zr)
  848. {
  849. struct snd_soc_component *component = mbhc->component;
  850. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  851. struct wcd939x_pdata *pdata = dev_get_platdata(wcd939x->dev);
  852. s16 reg0, reg1, reg2, reg3, reg4;
  853. uint32_t zdiff_val = 0, r_gnd_int_fet_mohms = 0, rl_eff_mohms = 0, r_gnd_ext_fet_mohms = 0;
  854. uint32_t aud_tap = 0, gnd_tap = 0;
  855. uint32_t *zdiff = &zdiff_val;
  856. int32_t z1L, z1R, z1Ls, z1Diff;
  857. int zMono, z_diff1, z_diff2;
  858. bool is_fsm_disable = false;
  859. struct wcd939x_mbhc_zdet_param zdet_param = {4, 0, 6, 0x18, 0x60, 0x78};
  860. struct wcd939x_mbhc_zdet_param *zdet_param_ptr = &zdet_param;
  861. s16 d1[] = {0, 30, 30, 6};
  862. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  863. /* Turn on RX supplies */
  864. if (wcd939x->version == WCD939X_VERSION_2_0) {
  865. /* Start up Buck/Flyback, Enable RX bias, Use MBHC RCO for MBHC Zdet, Enable Vneg */
  866. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x4C, 0x4C);
  867. /* Wait 100us for settling */
  868. usleep_range(100, 110);
  869. /* Enable VNEGDAC_LDO */
  870. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x10, 0x10);
  871. /* Wait 100us for settling */
  872. usleep_range(100, 110);
  873. /* Keep PA left/right channels disabled */
  874. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x01, 0x01);
  875. /* Enable VPOS */
  876. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x20, 0x20);
  877. /* Wait 500us for settling */
  878. usleep_range(500, 510);
  879. }
  880. /* Store register values */
  881. reg0 = snd_soc_component_read(component, WCD939X_MBHC_BTN5);
  882. reg1 = snd_soc_component_read(component, WCD939X_MBHC_BTN6);
  883. reg2 = snd_soc_component_read(component, WCD939X_MBHC_BTN7);
  884. reg3 = snd_soc_component_read(component, WCD939X_CTL_CLK);
  885. reg4 = snd_soc_component_read(component, WCD939X_ZDET_ANA_CTL);
  886. /* Disable the detection FSM */
  887. if (snd_soc_component_read(component, WCD939X_MBHC_ELECT) & 0x80) {
  888. is_fsm_disable = true;
  889. regmap_update_bits(wcd939x->regmap,
  890. WCD939X_MBHC_ELECT, 0x80, 0x00);
  891. }
  892. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  893. if (mbhc->hphl_swh)
  894. regmap_update_bits(wcd939x->regmap,
  895. WCD939X_MBHC_MECH, 0x80, 0x00);
  896. /* Turn off 100k pull down on HPHL */
  897. regmap_update_bits(wcd939x->regmap,
  898. WCD939X_MBHC_MECH, 0x01, 0x00);
  899. /* Disable surge protection before impedance detection.
  900. * This is done to give correct value for high impedance.
  901. */
  902. regmap_update_bits(wcd939x->regmap,
  903. WCD939X_HPHLR_SURGE_EN, 0xC0, 0x00);
  904. /* 1ms delay needed after disable surge protection */
  905. usleep_range(1000, 1010);
  906. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  907. /* Disable sense switch and MIC for USB-C analog platforms */
  908. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  909. wcd_usbss_set_switch_settings_enable(SENSE_SWITCHES, USBSS_SWITCH_DISABLE);
  910. wcd_usbss_set_switch_settings_enable(MIC_SWITCHES, USBSS_SWITCH_DISABLE);
  911. }
  912. #endif
  913. /* L-channel impedance */
  914. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  915. if ((z1L == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1L > WCD939X_ZDET_VAL_100K)) {
  916. *zl = WCD939X_ZDET_FLOATING_IMPEDANCE;
  917. } else {
  918. *zl = z1L;
  919. wcd939x_wcd_mbhc_qfuse_cal(component, zl, 0);
  920. }
  921. /* Differential measurement for USB-C analog platforms */
  922. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  923. dev_dbg(component->dev, "%s: effective impedance on HPH_L = %d(mohms)\n",
  924. __func__, *zl);
  925. goto diff_impedance;
  926. }
  927. dev_dbg(component->dev, "%s: impedance on HPH_L = %d(mohms)\n",
  928. __func__, *zl);
  929. /* R-channel impedance */
  930. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
  931. if ((z1R == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1R > WCD939X_ZDET_VAL_100K)) {
  932. *zr = WCD939X_ZDET_FLOATING_IMPEDANCE;
  933. } else {
  934. *zr = z1R;
  935. wcd939x_wcd_mbhc_qfuse_cal(component, zr, 1);
  936. }
  937. dev_dbg(component->dev, "%s: impedance on HPH_R = %d(mohms)\n",
  938. __func__, *zr);
  939. /* Convert from mohms to ohms (rounded) */
  940. *zl = (*zl + OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  941. *zr = (*zr + OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  942. goto mono_stereo_detection;
  943. diff_impedance:
  944. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  945. /* Disable AGND switch */
  946. wcd_usbss_set_switch_settings_enable(AGND_SWITCHES, USBSS_SWITCH_DISABLE);
  947. #endif
  948. /* Enable HPHR NCLAMP */
  949. regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_MISC1, 0x08, 0x08);
  950. /* Diffrential impedance */
  951. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1Diff, NULL, d1);
  952. if ((z1Diff == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1Diff > WCD939X_ZDET_VAL_100K)) {
  953. *zdiff = WCD939X_ZDET_FLOATING_IMPEDANCE;
  954. } else {
  955. *zdiff = z1Diff;
  956. wcd939x_wcd_mbhc_qfuse_cal(component, zdiff, 0);
  957. }
  958. dev_dbg(component->dev, "%s: effective impedance on HPH_diff after calib = %d(mohms)\n",
  959. __func__, *zdiff);
  960. /* Disable HPHR NCLAMP */
  961. regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_MISC1, 0x08, 0x00);
  962. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  963. /* Enable AGND switch */
  964. wcd_usbss_set_switch_settings_enable(AGND_SWITCHES, USBSS_SWITCH_ENABLE);
  965. /* Get ground internal resistance based on orientation */
  966. if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
  967. r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu2_int_fet_mohms;
  968. } else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
  969. r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu1_int_fet_mohms;
  970. } else {
  971. *zl = 0;
  972. *zr = 0;
  973. dev_dbg(component->dev, "%s: Invalid SBU switch orientation\n", __func__);
  974. goto zdet_complete;
  975. }
  976. #endif
  977. /* Compute external fet and effective load impedance */
  978. r_gnd_ext_fet_mohms = *zl - *zdiff / 2 + pdata->usbcss_hs.r_surge_mohms / 2 -
  979. pdata->usbcss_hs.r_gnd_par_tot_mohms - r_gnd_int_fet_mohms;
  980. rl_eff_mohms = *zdiff / 2 - pdata->usbcss_hs.r_aud_int_fet_r_mohms -
  981. pdata->usbcss_hs.r_gnd_ext_fet_mohms - pdata->usbcss_hs.r_surge_mohms / 2 -
  982. pdata->usbcss_hs.r_gnd_par_tot_mohms;
  983. /* Store values */
  984. *zl = (rl_eff_mohms - pdata->usbcss_hs.r_conn_par_load_pos_mohms - pdata->usbcss_hs.r3 +
  985. OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  986. *zr = *zl;
  987. /* Update USBC-SS HS params */
  988. if (rl_eff_mohms > MAX_RL_EFF_MOHMS)
  989. rl_eff_mohms = MAX_RL_EFF_MOHMS;
  990. else if (rl_eff_mohms == 0)
  991. rl_eff_mohms = MIN_RL_EFF_MOHMS;
  992. pdata->usbcss_hs.r_load_eff_l_mohms = rl_eff_mohms;
  993. pdata->usbcss_hs.r_load_eff_r_mohms = rl_eff_mohms;
  994. update_ext_fet_res(pdata, r_gnd_ext_fet_mohms);
  995. update_xtalk_scale_and_alpha(pdata, wcd939x->regmap);
  996. dev_dbg(component->dev, "%s: Xtalk scale is 0x%x and alpha is 0x%x\n",
  997. __func__, pdata->usbcss_hs.scale_l, pdata->usbcss_hs.alpha_l);
  998. get_linearizer_taps(pdata, &aud_tap, &gnd_tap);
  999. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1000. wcd_usbss_set_linearizer_sw_tap(aud_tap, gnd_tap);
  1001. #endif
  1002. dev_dbg(component->dev, "%s: Linearizer aud_tap is 0x%x and gnd_tap is 0x%x\n",
  1003. __func__, aud_tap, gnd_tap);
  1004. mono_stereo_detection:
  1005. /* Mono/stereo detection */
  1006. if ((*zl == WCD939X_ZDET_FLOATING_IMPEDANCE) && (*zr == WCD939X_ZDET_FLOATING_IMPEDANCE)) {
  1007. dev_dbg(component->dev,
  1008. "%s: plug type is invalid or extension cable\n",
  1009. __func__);
  1010. goto zdet_complete;
  1011. }
  1012. if ((*zl == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
  1013. (*zr == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
  1014. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  1015. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  1016. dev_dbg(component->dev,
  1017. "%s: Mono plug type with one ch floating or shorted to GND\n",
  1018. __func__);
  1019. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  1020. goto zdet_complete;
  1021. }
  1022. snd_soc_component_update_bits(component, WCD939X_R_ATEST, 0x02, 0x02);
  1023. snd_soc_component_update_bits(component, WCD939X_PA_CTL2, 0x40, 0x01);
  1024. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1Ls, NULL, d1);
  1025. snd_soc_component_update_bits(component, WCD939X_PA_CTL2, 0x40, 0x00);
  1026. snd_soc_component_update_bits(component, WCD939X_R_ATEST, 0x02, 0x00);
  1027. z1Ls /= 1000;
  1028. wcd939x_wcd_mbhc_qfuse_cal(component, &z1Ls, 0);
  1029. /* Parallel of left Z and 9 ohm pull down resistor */
  1030. zMono = ((*zl) * 9) / ((*zl) + 9);
  1031. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  1032. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  1033. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  1034. dev_dbg(component->dev, "%s: stereo plug type detected\n",
  1035. __func__);
  1036. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  1037. } else {
  1038. dev_dbg(component->dev, "%s: MONO plug type detected\n",
  1039. __func__);
  1040. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  1041. }
  1042. zdet_complete:
  1043. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1044. /* Enable sense switch and MIC for USB-C analog platforms */
  1045. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1046. wcd_usbss_set_switch_settings_enable(SENSE_SWITCHES, USBSS_SWITCH_ENABLE);
  1047. wcd_usbss_set_switch_settings_enable(MIC_SWITCHES, USBSS_SWITCH_ENABLE);
  1048. }
  1049. #endif
  1050. /* Enable surge protection again after impedance detection */
  1051. regmap_update_bits(wcd939x->regmap,
  1052. WCD939X_HPHLR_SURGE_EN, 0xC0, 0xC0);
  1053. snd_soc_component_write(component, WCD939X_MBHC_BTN5, reg0);
  1054. snd_soc_component_write(component, WCD939X_MBHC_BTN6, reg1);
  1055. snd_soc_component_write(component, WCD939X_MBHC_BTN7, reg2);
  1056. /* Turn on 100k pull down on HPHL */
  1057. regmap_update_bits(wcd939x->regmap,
  1058. WCD939X_MBHC_MECH, 0x01, 0x01);
  1059. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  1060. if (mbhc->hphl_swh)
  1061. regmap_update_bits(wcd939x->regmap,
  1062. WCD939X_MBHC_MECH, 0x80, 0x80);
  1063. snd_soc_component_write(component, WCD939X_ZDET_ANA_CTL, reg4);
  1064. snd_soc_component_write(component, WCD939X_CTL_CLK, reg3);
  1065. if (is_fsm_disable)
  1066. regmap_update_bits(wcd939x->regmap,
  1067. WCD939X_MBHC_ELECT, 0x80, 0x80);
  1068. /* Turn off RX supplies */
  1069. if (wcd939x->version == WCD939X_VERSION_2_0) {
  1070. /* Set VPOS to be controlled by RX */
  1071. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x20, 0x00);
  1072. /* Wait 500us for settling */
  1073. usleep_range(500, 510);
  1074. /* Set PA Left/Right channels and VNEGDAC_LDO to be controlled by RX */
  1075. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x11, 0x00);
  1076. /* Wait 100us for settling */
  1077. usleep_range(100, 110);
  1078. /* Set Vneg mode and enable to be controlled by RX */
  1079. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x06, 0x00);
  1080. /* Wait 100us for settling */
  1081. usleep_range(100, 110);
  1082. /* Set RX bias to be controlled by RX and set Buck/Flyback back to SWR Rx clock */
  1083. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x48, 0x00);
  1084. }
  1085. }
  1086. static void wcd939x_mbhc_gnd_det_ctrl(struct snd_soc_component *component,
  1087. bool enable)
  1088. {
  1089. if (enable) {
  1090. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1091. 0x02, 0x02);
  1092. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1093. 0x40, 0x40);
  1094. } else {
  1095. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1096. 0x40, 0x00);
  1097. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1098. 0x02, 0x00);
  1099. }
  1100. }
  1101. static void wcd939x_mbhc_hph_pull_down_ctrl(struct snd_soc_component *component,
  1102. bool enable)
  1103. {
  1104. if (enable) {
  1105. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1106. 0x40, 0x40);
  1107. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1108. 0x10, 0x10);
  1109. } else {
  1110. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1111. 0x40, 0x00);
  1112. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1113. 0x10, 0x00);
  1114. }
  1115. }
  1116. static void wcd939x_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  1117. {
  1118. struct snd_soc_component *component = mbhc->component;
  1119. if ((mbhc->moist_rref == R_OFF) ||
  1120. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  1121. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1122. 0x0C, R_OFF << 2);
  1123. return;
  1124. }
  1125. /* Do not enable moisture detection if jack type is NC */
  1126. if (!mbhc->hphl_swh) {
  1127. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  1128. __func__);
  1129. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1130. 0x0C, R_OFF << 2);
  1131. return;
  1132. }
  1133. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1134. 0x0C, mbhc->moist_rref << 2);
  1135. }
  1136. static void wcd939x_mbhc_moisture_detect_en(struct wcd_mbhc *mbhc, bool enable)
  1137. {
  1138. struct snd_soc_component *component = mbhc->component;
  1139. if (enable)
  1140. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1141. 0x0C, mbhc->moist_rref << 2);
  1142. else
  1143. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1144. 0x0C, R_OFF << 2);
  1145. }
  1146. static bool wcd939x_mbhc_get_moisture_status(struct wcd_mbhc *mbhc)
  1147. {
  1148. struct snd_soc_component *component = mbhc->component;
  1149. bool ret = false;
  1150. if ((mbhc->moist_rref == R_OFF) ||
  1151. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  1152. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1153. 0x0C, R_OFF << 2);
  1154. goto done;
  1155. }
  1156. /* Do not enable moisture detection if jack type is NC */
  1157. if (!mbhc->hphl_swh) {
  1158. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  1159. __func__);
  1160. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1161. 0x0C, R_OFF << 2);
  1162. goto done;
  1163. }
  1164. /*
  1165. * If moisture_en is already enabled, then skip to plug type
  1166. * detection.
  1167. */
  1168. if ((snd_soc_component_read(component, WCD939X_CTL_2) & 0x0C))
  1169. goto done;
  1170. wcd939x_mbhc_moisture_detect_en(mbhc, true);
  1171. /* Read moisture comparator status */
  1172. ret = ((snd_soc_component_read(component, WCD939X_FSM_STATUS)
  1173. & 0x20) ? 0 : 1);
  1174. done:
  1175. return ret;
  1176. }
  1177. static void wcd939x_mbhc_moisture_polling_ctrl(struct wcd_mbhc *mbhc,
  1178. bool enable)
  1179. {
  1180. struct snd_soc_component *component = mbhc->component;
  1181. snd_soc_component_update_bits(component,
  1182. WCD939X_MOISTURE_DET_POLLING_CTRL,
  1183. 0x04, (enable << 2));
  1184. }
  1185. static void wcd939x_mbhc_bcs_enable(struct wcd_mbhc *mbhc,
  1186. bool bcs_enable)
  1187. {
  1188. if (bcs_enable)
  1189. wcd939x_disable_bcs_before_slow_insert(mbhc->component, false);
  1190. else
  1191. wcd939x_disable_bcs_before_slow_insert(mbhc->component, true);
  1192. }
  1193. static const struct wcd_mbhc_cb mbhc_cb = {
  1194. .request_irq = wcd939x_mbhc_request_irq,
  1195. .irq_control = wcd939x_mbhc_irq_control,
  1196. .free_irq = wcd939x_mbhc_free_irq,
  1197. .clk_setup = wcd939x_mbhc_clk_setup,
  1198. .map_btn_code_to_num = wcd939x_mbhc_btn_to_num,
  1199. .mbhc_bias = wcd939x_mbhc_mbhc_bias_control,
  1200. .set_btn_thr = wcd939x_mbhc_program_btn_thr,
  1201. .lock_sleep = wcd939x_mbhc_lock_sleep,
  1202. .register_notifier = wcd939x_mbhc_register_notifier,
  1203. .micbias_enable_status = wcd939x_mbhc_micb_en_status,
  1204. .hph_pa_on_status = wcd939x_mbhc_hph_pa_on_status,
  1205. .hph_pull_up_control_v2 = wcd939x_mbhc_hph_l_pull_up_control,
  1206. .mbhc_micbias_control = wcd939x_mbhc_request_micbias,
  1207. .mbhc_micb_ramp_control = wcd939x_mbhc_micb_ramp_control,
  1208. .get_hwdep_fw_cal = wcd939x_get_hwdep_fw_cal,
  1209. .mbhc_micb_ctrl_thr_mic = wcd939x_mbhc_micb_ctrl_threshold_mic,
  1210. .compute_impedance = wcd939x_wcd_mbhc_calc_impedance,
  1211. .mbhc_gnd_det_ctrl = wcd939x_mbhc_gnd_det_ctrl,
  1212. .hph_pull_down_ctrl = wcd939x_mbhc_hph_pull_down_ctrl,
  1213. .mbhc_moisture_config = wcd939x_mbhc_moisture_config,
  1214. .mbhc_get_moisture_status = wcd939x_mbhc_get_moisture_status,
  1215. .mbhc_moisture_polling_ctrl = wcd939x_mbhc_moisture_polling_ctrl,
  1216. .mbhc_moisture_detect_en = wcd939x_mbhc_moisture_detect_en,
  1217. .bcs_enable = wcd939x_mbhc_bcs_enable,
  1218. };
  1219. static int wcd939x_get_hph_type(struct snd_kcontrol *kcontrol,
  1220. struct snd_ctl_elem_value *ucontrol)
  1221. {
  1222. struct snd_soc_component *component =
  1223. snd_soc_kcontrol_component(kcontrol);
  1224. struct wcd939x_mbhc *wcd939x_mbhc = wcd939x_soc_get_mbhc(component);
  1225. struct wcd_mbhc *mbhc;
  1226. if (!wcd939x_mbhc) {
  1227. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1228. return -EINVAL;
  1229. }
  1230. mbhc = &wcd939x_mbhc->wcd_mbhc;
  1231. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  1232. dev_dbg(component->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  1233. return 0;
  1234. }
  1235. static int wcd939x_hph_impedance_get(struct snd_kcontrol *kcontrol,
  1236. struct snd_ctl_elem_value *ucontrol)
  1237. {
  1238. uint32_t zl, zr;
  1239. bool hphr;
  1240. struct soc_multi_mixer_control *mc;
  1241. struct snd_soc_component *component =
  1242. snd_soc_kcontrol_component(kcontrol);
  1243. struct wcd939x_mbhc *wcd939x_mbhc = wcd939x_soc_get_mbhc(component);
  1244. if (!wcd939x_mbhc) {
  1245. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1246. return -EINVAL;
  1247. }
  1248. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1249. hphr = mc->shift;
  1250. wcd_mbhc_get_impedance(&wcd939x_mbhc->wcd_mbhc, &zl, &zr);
  1251. dev_dbg(component->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  1252. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  1253. return 0;
  1254. }
  1255. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  1256. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  1257. wcd939x_get_hph_type, NULL),
  1258. };
  1259. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  1260. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  1261. wcd939x_hph_impedance_get, NULL),
  1262. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  1263. wcd939x_hph_impedance_get, NULL),
  1264. };
  1265. /*
  1266. * wcd939x_mbhc_get_impedance: get impedance of headphone
  1267. * left and right channels
  1268. * @wcd939x_mbhc: handle to struct wcd939x_mbhc *
  1269. * @zl: handle to left-ch impedance
  1270. * @zr: handle to right-ch impedance
  1271. * return 0 for success or error code in case of failure
  1272. */
  1273. int wcd939x_mbhc_get_impedance(struct wcd939x_mbhc *wcd939x_mbhc,
  1274. uint32_t *zl, uint32_t *zr)
  1275. {
  1276. if (!wcd939x_mbhc) {
  1277. pr_err_ratelimited("%s: mbhc not initialized!\n", __func__);
  1278. return -EINVAL;
  1279. }
  1280. if (!zl || !zr) {
  1281. pr_err_ratelimited("%s: zl or zr null!\n", __func__);
  1282. return -EINVAL;
  1283. }
  1284. return wcd_mbhc_get_impedance(&wcd939x_mbhc->wcd_mbhc, zl, zr);
  1285. }
  1286. EXPORT_SYMBOL(wcd939x_mbhc_get_impedance);
  1287. /*
  1288. * wcd939x_mbhc_hs_detect: starts mbhc insertion/removal functionality
  1289. * @codec: handle to snd_soc_component *
  1290. * @mbhc_cfg: handle to mbhc configuration structure
  1291. * return 0 if mbhc_start is success or error code in case of failure
  1292. */
  1293. int wcd939x_mbhc_hs_detect(struct snd_soc_component *component,
  1294. struct wcd_mbhc_config *mbhc_cfg)
  1295. {
  1296. struct wcd939x_priv *wcd939x = NULL;
  1297. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  1298. if (!component) {
  1299. pr_err_ratelimited("%s: component is NULL\n", __func__);
  1300. return -EINVAL;
  1301. }
  1302. wcd939x = snd_soc_component_get_drvdata(component);
  1303. if (!wcd939x) {
  1304. pr_err_ratelimited("%s: wcd939x is NULL\n", __func__);
  1305. return -EINVAL;
  1306. }
  1307. wcd939x_mbhc = wcd939x->mbhc;
  1308. if (!wcd939x_mbhc) {
  1309. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1310. return -EINVAL;
  1311. }
  1312. return wcd_mbhc_start(&wcd939x_mbhc->wcd_mbhc, mbhc_cfg);
  1313. }
  1314. EXPORT_SYMBOL(wcd939x_mbhc_hs_detect);
  1315. /*
  1316. * wcd939x_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  1317. * @component: handle to snd_soc_component *
  1318. */
  1319. void wcd939x_mbhc_hs_detect_exit(struct snd_soc_component *component)
  1320. {
  1321. struct wcd939x_priv *wcd939x = NULL;
  1322. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  1323. if (!component) {
  1324. pr_err_ratelimited("%s: component is NULL\n", __func__);
  1325. return;
  1326. }
  1327. wcd939x = snd_soc_component_get_drvdata(component);
  1328. if (!wcd939x) {
  1329. pr_err_ratelimited("%s: wcd939x is NULL\n", __func__);
  1330. return;
  1331. }
  1332. wcd939x_mbhc = wcd939x->mbhc;
  1333. if (!wcd939x_mbhc) {
  1334. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1335. return;
  1336. }
  1337. wcd_mbhc_stop(&wcd939x_mbhc->wcd_mbhc);
  1338. }
  1339. EXPORT_SYMBOL(wcd939x_mbhc_hs_detect_exit);
  1340. /*
  1341. * wcd939x_mbhc_ssr_down: stop mbhc during
  1342. * wcd939x subsystem restart
  1343. * mbhc: pointer to wcd937x_mbhc structure
  1344. * component: handle to snd_soc_component *
  1345. */
  1346. void wcd939x_mbhc_ssr_down(struct wcd939x_mbhc *mbhc,
  1347. struct snd_soc_component *component)
  1348. {
  1349. struct wcd_mbhc *wcd_mbhc = NULL;
  1350. if (!mbhc || !component)
  1351. return;
  1352. wcd_mbhc = &mbhc->wcd_mbhc;
  1353. if (!wcd_mbhc) {
  1354. dev_err_ratelimited(component->dev, "%s: wcd_mbhc is NULL\n", __func__);
  1355. return;
  1356. }
  1357. wcd939x_mbhc_hs_detect_exit(component);
  1358. wcd_mbhc_deinit(wcd_mbhc);
  1359. }
  1360. EXPORT_SYMBOL(wcd939x_mbhc_ssr_down);
  1361. /*
  1362. * wcd939x_mbhc_post_ssr_init: initialize mbhc for
  1363. * wcd939x post subsystem restart
  1364. * @mbhc: poniter to wcd939x_mbhc structure
  1365. * @component: handle to snd_soc_component *
  1366. *
  1367. * return 0 if mbhc_init is success or error code in case of failure
  1368. */
  1369. int wcd939x_mbhc_post_ssr_init(struct wcd939x_mbhc *mbhc,
  1370. struct snd_soc_component *component)
  1371. {
  1372. int ret = 0;
  1373. struct wcd_mbhc *wcd_mbhc = NULL;
  1374. if (!mbhc || !component)
  1375. return -EINVAL;
  1376. wcd_mbhc = &mbhc->wcd_mbhc;
  1377. if (wcd_mbhc == NULL) {
  1378. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  1379. return -EINVAL;
  1380. }
  1381. /* Reset detection type to insertion after SSR recovery */
  1382. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1383. 0x20, 0x20);
  1384. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb, &intr_ids,
  1385. wcd_mbhc_registers, WCD939X_ZDET_SUPPORTED);
  1386. if (ret) {
  1387. dev_err(component->dev, "%s: mbhc initialization failed\n",
  1388. __func__);
  1389. goto done;
  1390. }
  1391. done:
  1392. return ret;
  1393. }
  1394. EXPORT_SYMBOL(wcd939x_mbhc_post_ssr_init);
  1395. /*
  1396. * wcd939x_mbhc_init: initialize mbhc for wcd939x
  1397. * @mbhc: poniter to wcd939x_mbhc struct pointer to store the configs
  1398. * @codec: handle to snd_soc_component *
  1399. * @fw_data: handle to firmware data
  1400. *
  1401. * return 0 if mbhc_init is success or error code in case of failure
  1402. */
  1403. int wcd939x_mbhc_init(struct wcd939x_mbhc **mbhc,
  1404. struct snd_soc_component *component,
  1405. struct fw_info *fw_data)
  1406. {
  1407. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  1408. struct wcd_mbhc *wcd_mbhc = NULL;
  1409. int ret = 0;
  1410. struct wcd939x_pdata *pdata;
  1411. if (!component) {
  1412. pr_err("%s: component is NULL\n", __func__);
  1413. return -EINVAL;
  1414. }
  1415. wcd939x_mbhc = devm_kzalloc(component->dev, sizeof(struct wcd939x_mbhc),
  1416. GFP_KERNEL);
  1417. if (!wcd939x_mbhc)
  1418. return -ENOMEM;
  1419. wcd939x_mbhc->fw_data = fw_data;
  1420. BLOCKING_INIT_NOTIFIER_HEAD(&wcd939x_mbhc->notifier);
  1421. wcd_mbhc = &wcd939x_mbhc->wcd_mbhc;
  1422. if (wcd_mbhc == NULL) {
  1423. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  1424. ret = -EINVAL;
  1425. goto err;
  1426. }
  1427. /* Setting default mbhc detection logic to ADC */
  1428. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  1429. pdata = dev_get_platdata(component->dev);
  1430. if (!pdata) {
  1431. dev_err(component->dev, "%s: pdata pointer is NULL\n",
  1432. __func__);
  1433. ret = -EINVAL;
  1434. goto err;
  1435. }
  1436. wcd_mbhc->micb_mv = pdata->micbias.micb2_mv;
  1437. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb,
  1438. &intr_ids, wcd_mbhc_registers,
  1439. WCD939X_ZDET_SUPPORTED);
  1440. if (ret) {
  1441. dev_err(component->dev, "%s: mbhc initialization failed\n",
  1442. __func__);
  1443. goto err;
  1444. }
  1445. (*mbhc) = wcd939x_mbhc;
  1446. snd_soc_add_component_controls(component, impedance_detect_controls,
  1447. ARRAY_SIZE(impedance_detect_controls));
  1448. snd_soc_add_component_controls(component, hph_type_detect_controls,
  1449. ARRAY_SIZE(hph_type_detect_controls));
  1450. return 0;
  1451. err:
  1452. devm_kfree(component->dev, wcd939x_mbhc);
  1453. return ret;
  1454. }
  1455. EXPORT_SYMBOL(wcd939x_mbhc_init);
  1456. /*
  1457. * wcd939x_mbhc_deinit: deinitialize mbhc for wcd939x
  1458. * @codec: handle to snd_soc_component *
  1459. */
  1460. void wcd939x_mbhc_deinit(struct snd_soc_component *component)
  1461. {
  1462. struct wcd939x_priv *wcd939x;
  1463. struct wcd939x_mbhc *wcd939x_mbhc;
  1464. if (!component) {
  1465. pr_err("%s: component is NULL\n", __func__);
  1466. return;
  1467. }
  1468. wcd939x = snd_soc_component_get_drvdata(component);
  1469. if (!wcd939x) {
  1470. pr_err("%s: wcd939x is NULL\n", __func__);
  1471. return;
  1472. }
  1473. wcd939x_mbhc = wcd939x->mbhc;
  1474. if (wcd939x_mbhc) {
  1475. wcd_mbhc_deinit(&wcd939x_mbhc->wcd_mbhc);
  1476. devm_kfree(component->dev, wcd939x_mbhc);
  1477. }
  1478. }
  1479. EXPORT_SYMBOL(wcd939x_mbhc_deinit);