wcd939x-mbhc.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/device.h>
  10. #include <linux/printk.h>
  11. #include <linux/ratelimit.h>
  12. #include <linux/kernel.h>
  13. #include <linux/gpio.h>
  14. #include <linux/delay.h>
  15. #include <linux/regmap.h>
  16. #include <linux/timer.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc.h>
  20. #include <sound/soc-dapm.h>
  21. #include <asoc/wcdcal-hwdep.h>
  22. #include <asoc/wcd-mbhc-v2-api.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/kobject.h>
  25. #include "wcd939x-registers.h"
  26. #include "internal.h"
  27. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  28. #include <linux/soc/qcom/wcd939x-i2c.h>
  29. #endif
  30. #define WCD939X_ZDET_SUPPORTED true
  31. /* Z value defined in milliohm */
  32. #define WCD939X_ZDET_VAL_32 32000
  33. #define WCD939X_ZDET_VAL_400 400000
  34. #define WCD939X_ZDET_VAL_1200 1200000
  35. #define WCD939X_ZDET_VAL_100K 100000000
  36. /* Z floating defined in ohms */
  37. #define WCD939X_ZDET_FLOATING_IMPEDANCE 0x0FFFFFFE
  38. #define WCD939X_ZDET_NUM_MEASUREMENTS 900
  39. #define WCD939X_MBHC_GET_C1(c) ((c & 0xC000) >> 14)
  40. #define WCD939X_MBHC_GET_X1(x) (x & 0x3FFF)
  41. /* Z value compared in milliOhm */
  42. #define WCD939X_MBHC_IS_SECOND_RAMP_REQUIRED(z) false
  43. #define WCD939X_MBHC_ZDET_CONST (1071 * 1024)
  44. #define WCD939X_MBHC_MOISTURE_RREF R_24_KOHM
  45. #define OHMS_TO_MILLIOHMS 1000
  46. #define SLOPE_FACTOR_SCALER 10000
  47. #define FLOAT_TO_FIXED_XTALK (1UL << 16)
  48. #define MAX_XTALK_ALPHA 255
  49. #define MIN_RL_EFF_MOHMS 1
  50. #define MAX_RL_EFF_MOHMS 900000
  51. #define HD2_CODE_BASE_VALUE 0x1D
  52. #define HD2_CODE_INV_RESOLUTION 4201025
  53. #define FLOAT_TO_FIXED_LINEARIZER (1UL << 12)
  54. #define MIN_TAP_OFFSET -1023
  55. #define MAX_TAP_OFFSET 1023
  56. #define MIN_TAP 0
  57. #define MAX_TAP 1023
  58. #define RDOWN_TIMER_PERIOD_MSEC 100
  59. #define WCD_USBSS_WRITE true
  60. #define WCD_USBSS_READ false
  61. #define ZDET_SE 0
  62. #define ZDET_DIFF 1
  63. #define WCD_USBSS_EXT_LIN_EN 0x3D
  64. #define WCD_USBSS_EXT_SW_CTRL_1 0x43
  65. #define WCD_USBSS_MG1_BIAS 0x25
  66. #define WCD_USBSS_MG2_BIAS 0x29
  67. #define SE_SLOPE_MEAS_BIAS 10000
  68. #define DIFF_SLOPE_MEAS_BIAS 20000
  69. #define XTALK_CH_REG_ADDR_DELTA 4
  70. #define NUM_DIFF_MEAS 2
  71. #define ZDET_SE_MAX_MOHMS 600000
  72. #define ZDET_ACC_LMT_MOHMS 100000
  73. #define R_CONN_PAR_LOAD_POS_MOHMS 7895
  74. #define LINEARIZER_DEFAULT_TAP 0xE8
  75. #define GND_EXT_FET_MAX_MOHMS 2000
  76. struct zdet_dnl_entry {
  77. u8 base_val_ohms;
  78. s16 se_corr_mohms;
  79. s16 diff_corr_mohms;
  80. };
  81. static const struct zdet_dnl_entry zdet_dnl_table[] = {
  82. { 0, 0, 0},
  83. { 5, 56, 13},
  84. { 10, 60, 34},
  85. { 15, 13, -4},
  86. { 20, 21, 14},
  87. { 25, -16, -20},
  88. { 30, 5, 7},
  89. { 35, -46, -90},
  90. { 40, -4, -17},
  91. { 45, -74, -40},
  92. { 50, -52, 3},
  93. { 55, -37, -5},
  94. { 60, 4, -79},
  95. { 65, -34, -82},
  96. { 70, -105, -33},
  97. { 75, -81, -55},
  98. { 80, -39, 34},
  99. { 85, -37, 46},
  100. { 90, -51, 81},
  101. { 95, 14, 132},
  102. {100, 101, 197},
  103. {105, 150, 247},
  104. {110, 217, 245},
  105. {115, 232, -189},
  106. {120, 201, -146},
  107. {125, -152, -121},
  108. {130, -157, -69},
  109. {135, -118, -72},
  110. {140, -54, -24},
  111. {145, -55, 51},
  112. };
  113. static struct wcd_mbhc_register
  114. wcd_mbhc_registers[WCD_MBHC_REG_FUNC_MAX] = {
  115. WCD_MBHC_REGISTER("WCD_MBHC_L_DET_EN",
  116. WCD939X_MBHC_MECH, 0x80, 7, 0),
  117. WCD_MBHC_REGISTER("WCD_MBHC_GND_DET_EN",
  118. WCD939X_MBHC_MECH, 0x40, 6, 0),
  119. WCD_MBHC_REGISTER("WCD_MBHC_MECH_DETECTION_TYPE",
  120. WCD939X_MBHC_MECH, 0x20, 5, 0),
  121. WCD_MBHC_REGISTER("WCD_MBHC_MIC_CLAMP_CTL",
  122. WCD939X_PLUG_DETECT_CTL, 0x30, 4, 0),
  123. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_DETECTION_TYPE",
  124. WCD939X_MBHC_ELECT, 0x08, 3, 0),
  125. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_CTRL",
  126. WCD939X_MECH_DET_CURRENT, 0x1F, 0, 0),
  127. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL",
  128. WCD939X_MBHC_MECH, 0x04, 2, 0),
  129. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PLUG_TYPE",
  130. WCD939X_MBHC_MECH, 0x10, 4, 0),
  131. WCD_MBHC_REGISTER("WCD_MBHC_GND_PLUG_TYPE",
  132. WCD939X_MBHC_MECH, 0x08, 3, 0),
  133. WCD_MBHC_REGISTER("WCD_MBHC_SW_HPH_LP_100K_TO_GND",
  134. WCD939X_MBHC_MECH, 0x01, 0, 0),
  135. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_SCHMT_ISRC",
  136. WCD939X_MBHC_ELECT, 0x06, 1, 0),
  137. WCD_MBHC_REGISTER("WCD_MBHC_FSM_EN",
  138. WCD939X_MBHC_ELECT, 0x80, 7, 0),
  139. WCD_MBHC_REGISTER("WCD_MBHC_INSREM_DBNC",
  140. WCD939X_PLUG_DETECT_CTL, 0x0F, 0, 0),
  141. WCD_MBHC_REGISTER("WCD_MBHC_BTN_DBNC",
  142. WCD939X_CTL_1, 0x03, 0, 0),
  143. WCD_MBHC_REGISTER("WCD_MBHC_HS_VREF",
  144. WCD939X_CTL_2, 0x03, 0, 0),
  145. WCD_MBHC_REGISTER("WCD_MBHC_HS_COMP_RESULT",
  146. WCD939X_MBHC_RESULT_3, 0x08, 3, 0),
  147. WCD_MBHC_REGISTER("WCD_MBHC_IN2P_CLAMP_STATE",
  148. WCD939X_MBHC_RESULT_3, 0x10, 4, 0),
  149. WCD_MBHC_REGISTER("WCD_MBHC_MIC_SCHMT_RESULT",
  150. WCD939X_MBHC_RESULT_3, 0x20, 5, 0),
  151. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_SCHMT_RESULT",
  152. WCD939X_MBHC_RESULT_3, 0x80, 7, 0),
  153. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_SCHMT_RESULT",
  154. WCD939X_MBHC_RESULT_3, 0x40, 6, 0),
  155. WCD_MBHC_REGISTER("WCD_MBHC_OCP_FSM_EN",
  156. WCD939X_HPH_OCP_CTL, 0x10, 4, 0),
  157. WCD_MBHC_REGISTER("WCD_MBHC_BTN_RESULT",
  158. WCD939X_MBHC_RESULT_3, 0x07, 0, 0),
  159. WCD_MBHC_REGISTER("WCD_MBHC_BTN_ISRC_CTL",
  160. WCD939X_MBHC_ELECT, 0x70, 4, 0),
  161. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_RESULT",
  162. WCD939X_MBHC_RESULT_3, 0xFF, 0, 0),
  163. WCD_MBHC_REGISTER("WCD_MBHC_MICB_CTRL",
  164. WCD939X_MICB2, 0xC0, 6, 0),
  165. WCD_MBHC_REGISTER("WCD_MBHC_HPH_CNP_WG_TIME",
  166. WCD939X_CNP_WG_TIME, 0xFF, 0, 0),
  167. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_PA_EN",
  168. WCD939X_HPH, 0x40, 6, 0),
  169. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PA_EN",
  170. WCD939X_HPH, 0x80, 7, 0),
  171. WCD_MBHC_REGISTER("WCD_MBHC_HPH_PA_EN",
  172. WCD939X_HPH, 0xC0, 6, 0),
  173. WCD_MBHC_REGISTER("WCD_MBHC_SWCH_LEVEL_REMOVE",
  174. WCD939X_MBHC_RESULT_3, 0x10, 4, 0),
  175. WCD_MBHC_REGISTER("WCD_MBHC_PULLDOWN_CTRL",
  176. 0, 0, 0, 0),
  177. WCD_MBHC_REGISTER("WCD_MBHC_ANC_DET_EN",
  178. WCD939X_CTL_BCS, 0x02, 1, 0),
  179. WCD_MBHC_REGISTER("WCD_MBHC_FSM_STATUS",
  180. WCD939X_FSM_STATUS, 0x01, 0, 0),
  181. WCD_MBHC_REGISTER("WCD_MBHC_MUX_CTL",
  182. WCD939X_CTL_2, 0x70, 4, 0),
  183. WCD_MBHC_REGISTER("WCD_MBHC_MOISTURE_STATUS",
  184. WCD939X_FSM_STATUS, 0x20, 5, 0),
  185. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_GND",
  186. WCD939X_PA_CTL2, 0x40, 6, 0),
  187. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_GND",
  188. WCD939X_PA_CTL2, 0x10, 4, 0),
  189. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_DET_EN",
  190. WCD939X_L_TEST, 0x01, 0, 0),
  191. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_DET_EN",
  192. WCD939X_R_TEST, 0x01, 0, 0),
  193. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_STATUS",
  194. WCD939X_INTR_STATUS_0, 0x80, 7, 0),
  195. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_STATUS",
  196. WCD939X_INTR_STATUS_0, 0x20, 5, 0),
  197. WCD_MBHC_REGISTER("WCD_MBHC_ADC_EN",
  198. WCD939X_CTL_1, 0x08, 3, 0),
  199. WCD_MBHC_REGISTER("WCD_MBHC_ADC_COMPLETE", WCD939X_FSM_STATUS,
  200. 0x40, 6, 0),
  201. WCD_MBHC_REGISTER("WCD_MBHC_ADC_TIMEOUT", WCD939X_FSM_STATUS,
  202. 0x80, 7, 0),
  203. WCD_MBHC_REGISTER("WCD_MBHC_ADC_RESULT", WCD939X_ADC_RESULT,
  204. 0xFF, 0, 0),
  205. WCD_MBHC_REGISTER("WCD_MBHC_MICB2_VOUT", WCD939X_MICB2, 0x3F, 0, 0),
  206. WCD_MBHC_REGISTER("WCD_MBHC_ADC_MODE",
  207. WCD939X_CTL_1, 0x10, 4, 0),
  208. WCD_MBHC_REGISTER("WCD_MBHC_DETECTION_DONE",
  209. WCD939X_CTL_1, 0x04, 2, 0),
  210. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_ISRC_EN",
  211. WCD939X_MBHC_ZDET, 0x02, 1, 0),
  212. };
  213. static const struct wcd_mbhc_intr intr_ids = {
  214. .mbhc_sw_intr = WCD939X_IRQ_MBHC_SW_DET,
  215. .mbhc_btn_press_intr = WCD939X_IRQ_MBHC_BUTTON_PRESS_DET,
  216. .mbhc_btn_release_intr = WCD939X_IRQ_MBHC_BUTTON_RELEASE_DET,
  217. .mbhc_hs_ins_intr = WCD939X_IRQ_MBHC_ELECT_INS_REM_LEG_DET,
  218. .mbhc_hs_rem_intr = WCD939X_IRQ_MBHC_ELECT_INS_REM_DET,
  219. .hph_left_ocp = WCD939X_IRQ_HPHL_OCP_INT,
  220. .hph_right_ocp = WCD939X_IRQ_HPHR_OCP_INT,
  221. };
  222. struct wcd939x_mbhc_zdet_param {
  223. u16 ldo_ctl;
  224. u16 noff;
  225. u16 nshift;
  226. u16 btn5;
  227. u16 btn6;
  228. u16 btn7;
  229. };
  230. static int wcd939x_mbhc_request_irq(struct snd_soc_component *component,
  231. int irq, irq_handler_t handler,
  232. const char *name, void *data)
  233. {
  234. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  235. return wcd_request_irq(&wcd939x->irq_info, irq, name, handler, data);
  236. }
  237. static void wcd939x_mbhc_irq_control(struct snd_soc_component *component,
  238. int irq, bool enable)
  239. {
  240. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  241. if (enable)
  242. wcd_enable_irq(&wcd939x->irq_info, irq);
  243. else
  244. wcd_disable_irq(&wcd939x->irq_info, irq);
  245. }
  246. static int wcd939x_mbhc_free_irq(struct snd_soc_component *component,
  247. int irq, void *data)
  248. {
  249. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  250. wcd_free_irq(&wcd939x->irq_info, irq, data);
  251. return 0;
  252. }
  253. static void wcd939x_mbhc_clk_setup(struct snd_soc_component *component,
  254. bool enable)
  255. {
  256. if (enable)
  257. snd_soc_component_update_bits(component, WCD939X_CTL_1,
  258. 0x80, 0x80);
  259. else
  260. snd_soc_component_update_bits(component, WCD939X_CTL_1,
  261. 0x80, 0x00);
  262. }
  263. static int wcd939x_mbhc_btn_to_num(struct snd_soc_component *component)
  264. {
  265. return snd_soc_component_read(component, WCD939X_MBHC_RESULT_3) & 0x7;
  266. }
  267. static void wcd939x_mbhc_mbhc_bias_control(struct snd_soc_component *component,
  268. bool enable)
  269. {
  270. if (enable)
  271. snd_soc_component_update_bits(component, WCD939X_MBHC_ELECT,
  272. 0x01, 0x01);
  273. else
  274. snd_soc_component_update_bits(component, WCD939X_MBHC_ELECT,
  275. 0x01, 0x00);
  276. }
  277. static void wcd939x_mbhc_program_btn_thr(struct snd_soc_component *component,
  278. s16 *btn_low, s16 *btn_high,
  279. int num_btn, bool is_micbias)
  280. {
  281. int i;
  282. int vth;
  283. if (num_btn > WCD_MBHC_DEF_BUTTONS) {
  284. dev_err_ratelimited(component->dev, "%s: invalid number of buttons: %d\n",
  285. __func__, num_btn);
  286. return;
  287. }
  288. for (i = 0; i < num_btn; i++) {
  289. vth = ((btn_high[i] * 2) / 25) & 0x3F;
  290. snd_soc_component_update_bits(component, WCD939X_MBHC_BTN0 + i,
  291. 0xFC, vth << 2);
  292. dev_dbg(component->dev, "%s: btn_high[%d]: %d, vth: %d\n",
  293. __func__, i, btn_high[i], vth);
  294. }
  295. }
  296. static bool wcd939x_mbhc_lock_sleep(struct wcd_mbhc *mbhc, bool lock)
  297. {
  298. struct snd_soc_component *component = mbhc->component;
  299. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  300. wcd939x->wakeup((void*)wcd939x, lock);
  301. return true;
  302. }
  303. static int wcd939x_mbhc_register_notifier(struct wcd_mbhc *mbhc,
  304. struct notifier_block *nblock,
  305. bool enable)
  306. {
  307. struct wcd939x_mbhc *wcd939x_mbhc;
  308. wcd939x_mbhc = container_of(mbhc, struct wcd939x_mbhc, wcd_mbhc);
  309. if (enable)
  310. return blocking_notifier_chain_register(&wcd939x_mbhc->notifier,
  311. nblock);
  312. else
  313. return blocking_notifier_chain_unregister(
  314. &wcd939x_mbhc->notifier, nblock);
  315. }
  316. static bool wcd939x_mbhc_micb_en_status(struct wcd_mbhc *mbhc, int micb_num)
  317. {
  318. u8 val = 0;
  319. if (micb_num == MIC_BIAS_2) {
  320. val = ((snd_soc_component_read(mbhc->component,
  321. WCD939X_MICB2) & 0xC0)
  322. >> 6);
  323. if (val == 0x01)
  324. return true;
  325. }
  326. return false;
  327. }
  328. static bool wcd939x_mbhc_hph_pa_on_status(struct snd_soc_component *component)
  329. {
  330. return (snd_soc_component_read(component, WCD939X_HPH) & 0xC0) ?
  331. true : false;
  332. }
  333. static void wcd939x_mbhc_hph_l_pull_up_control(
  334. struct snd_soc_component *component,
  335. int pull_up_cur)
  336. {
  337. /* Default pull up current to 2uA */
  338. if (pull_up_cur > HS_PULLUP_I_OFF || pull_up_cur < HS_PULLUP_I_3P0_UA ||
  339. pull_up_cur == HS_PULLUP_I_DEFAULT)
  340. pull_up_cur = HS_PULLUP_I_2P0_UA;
  341. dev_dbg(component->dev, "%s: HS pull up current:%d\n",
  342. __func__, pull_up_cur);
  343. snd_soc_component_update_bits(component,
  344. WCD939X_MECH_DET_CURRENT,
  345. 0x1F, pull_up_cur);
  346. }
  347. static int wcd939x_mbhc_request_micbias(struct snd_soc_component *component,
  348. int micb_num, int req)
  349. {
  350. int ret = 0;
  351. ret = wcd939x_micbias_control(component, micb_num, req, false);
  352. return ret;
  353. }
  354. static void wcd939x_mbhc_micb_ramp_control(struct snd_soc_component *component,
  355. bool enable)
  356. {
  357. if (enable) {
  358. snd_soc_component_update_bits(component, WCD939X_MICB2_RAMP,
  359. 0x1C, 0x0C);
  360. snd_soc_component_update_bits(component, WCD939X_MICB2_RAMP,
  361. 0x80, 0x80);
  362. } else {
  363. snd_soc_component_update_bits(component, WCD939X_MICB2_RAMP,
  364. 0x80, 0x00);
  365. snd_soc_component_update_bits(component, WCD939X_MICB2_RAMP,
  366. 0x1C, 0x00);
  367. }
  368. }
  369. static struct firmware_cal *wcd939x_get_hwdep_fw_cal(struct wcd_mbhc *mbhc,
  370. enum wcd_cal_type type)
  371. {
  372. struct wcd939x_mbhc *wcd939x_mbhc;
  373. struct firmware_cal *hwdep_cal;
  374. struct snd_soc_component *component = mbhc->component;
  375. wcd939x_mbhc = container_of(mbhc, struct wcd939x_mbhc, wcd_mbhc);
  376. if (!component) {
  377. pr_err_ratelimited("%s: NULL component pointer\n", __func__);
  378. return NULL;
  379. }
  380. hwdep_cal = wcdcal_get_fw_cal(wcd939x_mbhc->fw_data, type);
  381. if (!hwdep_cal)
  382. dev_err_ratelimited(component->dev, "%s: cal not sent by %d\n",
  383. __func__, type);
  384. return hwdep_cal;
  385. }
  386. static int wcd939x_mbhc_micb_ctrl_threshold_mic(
  387. struct snd_soc_component *component,
  388. int micb_num, bool req_en)
  389. {
  390. struct wcd939x_pdata *pdata = dev_get_platdata(component->dev);
  391. int rc, micb_mv;
  392. if (micb_num != MIC_BIAS_2)
  393. return -EINVAL;
  394. /*
  395. * If device tree micbias level is already above the minimum
  396. * voltage needed to detect threshold microphone, then do
  397. * not change the micbias, just return.
  398. */
  399. if (pdata->micbias.micb2_mv >= WCD_MBHC_THR_HS_MICB_MV)
  400. return 0;
  401. micb_mv = req_en ? WCD_MBHC_THR_HS_MICB_MV : pdata->micbias.micb2_mv;
  402. rc = wcd939x_mbhc_micb_adjust_voltage(component, micb_mv, MIC_BIAS_2);
  403. return rc;
  404. }
  405. static inline void wcd939x_mbhc_get_result_params(struct wcd939x_priv *wcd939x,
  406. s16 *d1_a, u16 noff,
  407. int32_t *zdet)
  408. {
  409. int i;
  410. int val, val1;
  411. s16 c1;
  412. s32 x1, d1;
  413. int32_t denom;
  414. int minCode_param[] = {
  415. 3277, 1639, 820, 410, 205, 103, 52, 26
  416. };
  417. struct wcd939x_mbhc *wcd939x_mbhc = wcd939x->mbhc;
  418. regmap_update_bits(wcd939x->regmap, WCD939X_MBHC_ZDET, 0x20, 0x20);
  419. for (i = 0; i < WCD939X_ZDET_NUM_MEASUREMENTS; i++) {
  420. regmap_read(wcd939x->regmap, WCD939X_MBHC_RESULT_2, &val);
  421. if (val & 0x80)
  422. break;
  423. }
  424. val = val << 0x8;
  425. regmap_read(wcd939x->regmap, WCD939X_MBHC_RESULT_1, &val1);
  426. val |= val1;
  427. wcd939x_mbhc->rdown_prev_iter = val;
  428. regmap_update_bits(wcd939x->regmap, WCD939X_MBHC_ZDET, 0x20, 0x00);
  429. x1 = WCD939X_MBHC_GET_X1(val);
  430. c1 = WCD939X_MBHC_GET_C1(val);
  431. /* If ramp is not complete, give additional 5ms */
  432. if ((c1 < 2) && x1)
  433. usleep_range(5000, 5050);
  434. if (!c1 || !x1) {
  435. dev_dbg(wcd939x->dev,
  436. "%s: Impedance detect ramp error, c1=%d, x1=0x%x\n",
  437. __func__, c1, x1);
  438. goto ramp_down;
  439. }
  440. d1 = d1_a[c1];
  441. denom = (x1 * d1) - (1 << (14 - noff));
  442. if (denom > 0)
  443. *zdet = (WCD939X_MBHC_ZDET_CONST * 1000) / denom;
  444. else if (x1 < minCode_param[noff])
  445. *zdet = WCD939X_ZDET_FLOATING_IMPEDANCE;
  446. dev_dbg(wcd939x->dev, "%s: d1=%d, c1=%d, x1=0x%x, z_val=%d(milliOhm)\n",
  447. __func__, d1, c1, x1, *zdet);
  448. ramp_down:
  449. i = 0;
  450. wcd939x_mbhc->rdown_timer_complete = false;
  451. mod_timer(&wcd939x_mbhc->rdown_timer, jiffies + msecs_to_jiffies(RDOWN_TIMER_PERIOD_MSEC));
  452. while (x1) {
  453. regmap_read(wcd939x->regmap,
  454. WCD939X_MBHC_RESULT_1, &val);
  455. regmap_read(wcd939x->regmap,
  456. WCD939X_MBHC_RESULT_2, &val1);
  457. val = val << 0x08;
  458. val |= val1;
  459. x1 = WCD939X_MBHC_GET_X1(val);
  460. i++;
  461. if (i == WCD939X_ZDET_NUM_MEASUREMENTS)
  462. break;
  463. if (wcd939x_mbhc->rdown_timer_complete && wcd939x_mbhc->rdown_prev_iter == val)
  464. break;
  465. wcd939x_mbhc->rdown_prev_iter = val;
  466. }
  467. del_timer(&wcd939x_mbhc->rdown_timer);
  468. }
  469. static void wcd939x_mbhc_zdet_ramp(struct snd_soc_component *component,
  470. struct wcd939x_mbhc_zdet_param *zdet_param,
  471. int32_t *zl, int32_t *zr, s16 *d1_a)
  472. {
  473. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  474. int32_t zdet = 0;
  475. snd_soc_component_update_bits(component, WCD939X_ZDET_ANA_CTL, 0xF0,
  476. 0x80 | (zdet_param->ldo_ctl << 4));
  477. snd_soc_component_update_bits(component, WCD939X_MBHC_BTN5, 0xFC,
  478. zdet_param->btn5);
  479. snd_soc_component_update_bits(component, WCD939X_MBHC_BTN6, 0xFC,
  480. zdet_param->btn6);
  481. snd_soc_component_update_bits(component, WCD939X_MBHC_BTN7, 0xFC,
  482. zdet_param->btn7);
  483. snd_soc_component_update_bits(component, WCD939X_ZDET_ANA_CTL,
  484. 0x0F, zdet_param->noff);
  485. snd_soc_component_update_bits(component, WCD939X_ZDET_RAMP_CTL,
  486. 0x0F, zdet_param->nshift);
  487. snd_soc_component_update_bits(component, WCD939X_ZDET_RAMP_CTL,
  488. 0x70, 0x60); /*acc1_min_63 */
  489. if (!zl)
  490. goto z_right;
  491. /* Start impedance measurement for HPH_L */
  492. regmap_update_bits(wcd939x->regmap,
  493. WCD939X_MBHC_ZDET, 0x80, 0x80);
  494. dev_dbg(wcd939x->dev, "%s: ramp for HPH_L, noff = %d\n",
  495. __func__, zdet_param->noff);
  496. wcd939x_mbhc_get_result_params(wcd939x, d1_a, zdet_param->noff, &zdet);
  497. regmap_update_bits(wcd939x->regmap,
  498. WCD939X_MBHC_ZDET, 0x80, 0x00);
  499. *zl = zdet;
  500. z_right:
  501. if (!zr)
  502. return;
  503. /* Start impedance measurement for HPH_R */
  504. regmap_update_bits(wcd939x->regmap,
  505. WCD939X_MBHC_ZDET, 0x40, 0x40);
  506. dev_dbg(wcd939x->dev, "%s: ramp for HPH_R, noff = %d\n",
  507. __func__, zdet_param->noff);
  508. wcd939x_mbhc_get_result_params(wcd939x, d1_a, zdet_param->noff, &zdet);
  509. regmap_update_bits(wcd939x->regmap,
  510. WCD939X_MBHC_ZDET, 0x40, 0x00);
  511. *zr = zdet;
  512. }
  513. static inline void wcd939x_wcd_mbhc_qfuse_cal(
  514. struct snd_soc_component *component,
  515. int32_t *z_val, int flag_l_r)
  516. {
  517. s16 q1;
  518. int q1_cal;
  519. q1 = snd_soc_component_read(component,
  520. WCD939X_EFUSE_REG_21 + flag_l_r);
  521. if (q1 & 0x80)
  522. q1_cal = (10000 - ((q1 & 0x7F) * 10));
  523. else
  524. q1_cal = (10000 + (q1 * 10));
  525. if (q1_cal > 0) {
  526. if (*z_val < 200 * OHMS_TO_MILLIOHMS)
  527. *z_val = ((*z_val) * 10000) / q1_cal;
  528. else if (*z_val < 2000 * OHMS_TO_MILLIOHMS)
  529. *z_val = ((*z_val) * 1000) / q1_cal * 10;
  530. else if (*z_val < 20000 * OHMS_TO_MILLIOHMS)
  531. *z_val = ((*z_val) * 100) / q1_cal * 100;
  532. }
  533. }
  534. static void rdown_timer_callback(struct timer_list *timer)
  535. {
  536. struct wcd939x_mbhc *wcd939x_mbhc = container_of(timer, struct wcd939x_mbhc, rdown_timer);
  537. wcd939x_mbhc->rdown_timer_complete = true;
  538. }
  539. static void update_hd2_codes(struct regmap *regmap, u32 r_gnd_res_tot_mohms, u32 r_load_eff_mohms)
  540. {
  541. u64 hd2_delta = 0;
  542. if (!regmap)
  543. return;
  544. hd2_delta = (HD2_CODE_INV_RESOLUTION * (u64) r_gnd_res_tot_mohms +
  545. FLOAT_TO_FIXED_XTALK * (u64) ((r_gnd_res_tot_mohms + r_load_eff_mohms) / 2)) /
  546. (FLOAT_TO_FIXED_XTALK * (u64) (r_gnd_res_tot_mohms + r_load_eff_mohms));
  547. if (hd2_delta >= HD2_CODE_BASE_VALUE) {
  548. regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_L, 0x1F, 0x00);
  549. regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_R, 0x1F, 0x00);
  550. } else {
  551. regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_L, 0x1F,
  552. HD2_CODE_BASE_VALUE - hd2_delta);
  553. regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_R, 0x1F,
  554. HD2_CODE_BASE_VALUE - hd2_delta);
  555. }
  556. }
  557. static u8 get_xtalk_scale(u32 gain)
  558. {
  559. u8 i;
  560. int target, residue;
  561. if (gain == 0)
  562. return MAX_XTALK_SCALE;
  563. target = FLOAT_TO_FIXED_XTALK / ((int) gain);
  564. residue = target;
  565. for (i = 0; i <= MAX_XTALK_SCALE; i++) {
  566. residue = target - (1 << ((int)((u32) i)));
  567. if (residue < 0)
  568. return i;
  569. }
  570. return MAX_XTALK_SCALE;
  571. }
  572. static u8 get_xtalk_alpha(u32 gain, u8 scale)
  573. {
  574. u32 two_exp_scale, round_offset, alpha;
  575. if (gain == 0)
  576. return MIN_XTALK_ALPHA;
  577. two_exp_scale = 1 << ((u32) scale);
  578. round_offset = FLOAT_TO_FIXED_XTALK / 2;
  579. alpha = (((gain * two_exp_scale - FLOAT_TO_FIXED_XTALK) * 255) + round_offset)
  580. / FLOAT_TO_FIXED_XTALK;
  581. return (alpha <= MAX_XTALK_ALPHA) ? ((u8) alpha) : MAX_XTALK_ALPHA;
  582. }
  583. static void update_xtalk_scale_and_alpha(struct wcd939x_priv *wcd939x)
  584. {
  585. u32 r_gnd_res_tot_mohms = 0, r_gnd_int_fet_mohms = 0, r_gnd_par_route1_mohms = 0;
  586. u32 xtalk_gain_l = 0, xtalk_gain_r = 0, r_load_eff_mohms = 0;
  587. u32 xtalk_gain_denom_l = 0, xtalk_gain_denom_r = 0, r7 = 0;
  588. struct wcd939x_pdata *pdata = dev_get_platdata(wcd939x->dev);
  589. if (!pdata || pdata->usbcss_hs.xtalk.xtalk_config == XTALK_NONE)
  590. return;
  591. /* Default xtalk values */
  592. pdata->usbcss_hs.xtalk.scale_l = MAX_XTALK_SCALE;
  593. pdata->usbcss_hs.xtalk.alpha_l = MIN_XTALK_ALPHA;
  594. pdata->usbcss_hs.xtalk.scale_r = MAX_XTALK_SCALE;
  595. pdata->usbcss_hs.xtalk.alpha_r = MIN_XTALK_ALPHA;
  596. /* Orientation-dependent ground impedance parameters */
  597. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  598. if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
  599. r_gnd_res_tot_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_res_tot_mohms;
  600. r_gnd_int_fet_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_int_fet_mohms;
  601. r_gnd_par_route1_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_par_route1_mohms;
  602. r7 = pdata->usbcss_hs.gnd.sbu2.r7;
  603. } else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
  604. r_gnd_res_tot_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_res_tot_mohms;
  605. r_gnd_int_fet_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_int_fet_mohms;
  606. r_gnd_par_route1_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_par_route1_mohms;
  607. r7 = pdata->usbcss_hs.gnd.sbu1.r7;
  608. } else {
  609. dev_dbg(wcd939x->dev, "%s: Using default scale and alpha values\n", __func__);
  610. return;
  611. }
  612. #endif
  613. r_load_eff_mohms = (pdata->usbcss_hs.aud.l.r_load_eff_mohms +
  614. pdata->usbcss_hs.aud.r.r_load_eff_mohms) / 2;
  615. if (pdata->usbcss_hs.xtalk.xtalk_config == XTALK_ANALOG) {
  616. /* Update HD2 codes for analog xtalk */
  617. update_hd2_codes(wcd939x->regmap, r_gnd_res_tot_mohms, r_load_eff_mohms);
  618. }
  619. /* Left channel */
  620. xtalk_gain_denom_l = pdata->usbcss_hs.aud.l.zval - r_gnd_int_fet_mohms -
  621. r_gnd_par_route1_mohms + pdata->usbcss_hs.aud.l.r1;
  622. if (xtalk_gain_denom_l == 0) {
  623. dev_dbg(wcd939x->dev,
  624. "%s: Using default scale and alpha values for the left channel\n",
  625. __func__);
  626. } else {
  627. xtalk_gain_l = FLOAT_TO_FIXED_XTALK * pdata->usbcss_hs.gnd.r_common_gnd_mohms /
  628. xtalk_gain_denom_l;
  629. /* Store scale and alpha values */
  630. pdata->usbcss_hs.xtalk.scale_l = get_xtalk_scale(xtalk_gain_l);
  631. pdata->usbcss_hs.xtalk.alpha_l = get_xtalk_alpha(xtalk_gain_l,
  632. pdata->usbcss_hs.xtalk.scale_l);
  633. }
  634. /* Right channel */
  635. xtalk_gain_denom_r = pdata->usbcss_hs.aud.r.zval - r_gnd_int_fet_mohms -
  636. r_gnd_par_route1_mohms + pdata->usbcss_hs.aud.r.r1;
  637. if (xtalk_gain_denom_r == 0) {
  638. dev_dbg(wcd939x->dev,
  639. "%s: Using default scale and alpha values for the right channel\n",
  640. __func__);
  641. } else {
  642. xtalk_gain_r = FLOAT_TO_FIXED_XTALK * pdata->usbcss_hs.gnd.r_common_gnd_mohms /
  643. xtalk_gain_denom_r;
  644. pdata->usbcss_hs.xtalk.scale_r = get_xtalk_scale(xtalk_gain_r);
  645. pdata->usbcss_hs.xtalk.alpha_r = get_xtalk_alpha(xtalk_gain_r,
  646. pdata->usbcss_hs.xtalk.scale_r);
  647. }
  648. /* Print relevant values */
  649. dev_dbg(wcd939x->dev, "%s: %s = %dmohms, %s = %dmohms, %s = %dmohms\n", __func__,
  650. "Left SE measurement", pdata->usbcss_hs.aud.l.zval,
  651. "right SE measurment", pdata->usbcss_hs.aud.r.zval,
  652. "differential measurement", pdata->usbcss_hs.zdiffval);
  653. dev_dbg(wcd939x->dev,
  654. "%s: %s = %dmohms, %s = %dmohms, %s = %dmohms, %s = %dmohms, %s = %dmohms\n",
  655. __func__, "R1_L", pdata->usbcss_hs.aud.l.r1, "R1_R", pdata->usbcss_hs.aud.r.r1,
  656. "R7", r7, "r_gnd_int_fet_mohms", r_gnd_int_fet_mohms, "r_common_gnd_mohms",
  657. pdata->usbcss_hs.gnd.r_common_gnd_mohms);
  658. dev_dbg(wcd939x->dev, "%s: %s = %d, %s = %d %s %d\n", __func__,
  659. "Xtalk gain (L->R)", xtalk_gain_l, "xtalk gain (R->L)", xtalk_gain_r,
  660. ". To convert xtalk gain to floating point, divide by", FLOAT_TO_FIXED_XTALK);
  661. }
  662. static void update_ext_fet_res(struct wcd939x_pdata *pdata, u32 r_aud_ext_fet_mohms,
  663. u32 r_gnd_ext_fet_mohms)
  664. {
  665. if (!pdata)
  666. return;
  667. pdata->usbcss_hs.gnd.r_gnd_ext_fet_mohms = (r_gnd_ext_fet_mohms >
  668. MAX_USBCSS_HS_IMPEDANCE_MOHMS)
  669. ? MAX_USBCSS_HS_IMPEDANCE_MOHMS
  670. : r_gnd_ext_fet_mohms;
  671. pdata->usbcss_hs.aud.l.r_aud_ext_fet_mohms = (r_aud_ext_fet_mohms >
  672. MAX_USBCSS_HS_IMPEDANCE_MOHMS)
  673. ? MAX_USBCSS_HS_IMPEDANCE_MOHMS
  674. : r_aud_ext_fet_mohms;
  675. pdata->usbcss_hs.aud.r.r_aud_ext_fet_mohms = pdata->usbcss_hs.aud.l.r_aud_ext_fet_mohms;
  676. pdata->usbcss_hs.gnd.sbu1.r_gnd_res_tot_mohms = get_r_gnd_res_tot_mohms(
  677. pdata->usbcss_hs.gnd.sbu1.r_gnd_int_fet_mohms,
  678. pdata->usbcss_hs.gnd.r_gnd_ext_fet_mohms,
  679. pdata->usbcss_hs.gnd.sbu1.r_gnd_par_tot_mohms);
  680. pdata->usbcss_hs.gnd.sbu2.r_gnd_res_tot_mohms = get_r_gnd_res_tot_mohms(
  681. pdata->usbcss_hs.gnd.sbu2.r_gnd_int_fet_mohms,
  682. pdata->usbcss_hs.gnd.r_gnd_ext_fet_mohms,
  683. pdata->usbcss_hs.gnd.sbu2.r_gnd_par_tot_mohms);
  684. pdata->usbcss_hs.aud.l.r_aud_res_tot_mohms = get_r_aud_res_tot_mohms(
  685. pdata->usbcss_hs.aud.l.r_aud_int_fet_mohms,
  686. pdata->usbcss_hs.aud.l.r_aud_ext_fet_mohms,
  687. pdata->usbcss_hs.aud.l.r_load_eff_mohms);
  688. pdata->usbcss_hs.aud.r.r_aud_res_tot_mohms = get_r_aud_res_tot_mohms(
  689. pdata->usbcss_hs.aud.r.r_aud_int_fet_mohms,
  690. pdata->usbcss_hs.aud.r.r_aud_ext_fet_mohms,
  691. pdata->usbcss_hs.aud.r.r_load_eff_mohms);
  692. }
  693. static void get_linearizer_taps(struct wcd939x_pdata *pdata, u32 *aud_tap)
  694. {
  695. u32 r_gnd_int_fet_mohms = 0, r_gnd_par_tot_mohms = 0;
  696. u32 v_aud1 = 0, v_aud2 = 0, aud_denom = 0;
  697. u32 r_load_eff_mohms = 0, r3 = 0, r_aud_ext_fet_mohms = 0, r_aud_int_fet_mohms = 0;
  698. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  699. u32 r_gnd_res_tot_mohms = 0;
  700. #endif
  701. if (!pdata)
  702. goto err_data;
  703. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  704. /* Orientation-dependent ground impedance parameters */
  705. if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
  706. r_gnd_res_tot_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_res_tot_mohms;
  707. r_gnd_int_fet_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_int_fet_mohms;
  708. } else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
  709. r_gnd_res_tot_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_res_tot_mohms;
  710. r_gnd_int_fet_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_int_fet_mohms;
  711. } else {
  712. goto err_data;
  713. }
  714. #endif
  715. r_load_eff_mohms = (pdata->usbcss_hs.aud.l.r_load_eff_mohms +
  716. pdata->usbcss_hs.aud.r.r_load_eff_mohms) / 2;
  717. r3 = (pdata->usbcss_hs.aud.l.r3 + pdata->usbcss_hs.aud.r.r3) / 2;
  718. r_aud_ext_fet_mohms = (pdata->usbcss_hs.aud.l.r_aud_ext_fet_mohms +
  719. pdata->usbcss_hs.aud.l.r_aud_ext_fet_mohms) / 2;
  720. r_aud_int_fet_mohms = (pdata->usbcss_hs.aud.l.r_aud_int_fet_mohms +
  721. pdata->usbcss_hs.aud.l.r_aud_int_fet_mohms) / 2;
  722. aud_denom = (u32) (FLOAT_TO_FIXED_LINEARIZER +
  723. (FLOAT_TO_FIXED_LINEARIZER * pdata->usbcss_hs.aud.k_aud_times_100 / 100));
  724. v_aud2 = r_load_eff_mohms - r3 + r_gnd_int_fet_mohms +
  725. pdata->usbcss_hs.gnd.r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms;
  726. v_aud1 = v_aud2 + r_aud_ext_fet_mohms;
  727. v_aud1 = FLOAT_TO_FIXED_LINEARIZER * v_aud1 / (v_aud1 + r_aud_int_fet_mohms);
  728. v_aud2 = FLOAT_TO_FIXED_LINEARIZER * v_aud2 /
  729. (v_aud2 + r_aud_ext_fet_mohms + r_aud_int_fet_mohms);
  730. *aud_tap = (u32) ((s32) ((1000 * v_aud1 + 10 * pdata->usbcss_hs.aud.k_aud_times_100 * v_aud2
  731. + aud_denom / 2) / aud_denom) +
  732. pdata->usbcss_hs.aud.aud_tap_offset);
  733. if (*aud_tap > MAX_TAP)
  734. *aud_tap = MAX_TAP;
  735. else if (*aud_tap < MIN_TAP)
  736. *aud_tap = MIN_TAP;
  737. return;
  738. err_data:
  739. *aud_tap = LINEARIZER_DEFAULT_TAP;
  740. }
  741. static void interpolate_zdet_val(uint32_t *z, s64 z_meas_bias_removed, s64 z_val_slope_corrected,
  742. int lb, int flag_se_diff)
  743. {
  744. s64 lb_to_z = 0, lb_to_ub = 0, z_to_ub = 0, lb_corr = 0, ub_corr = 0, z_interp = 0;
  745. if (lb < 0)
  746. return;
  747. /* If lb is the table upper bound, no interpolation needed, just use the lb corr factor */
  748. if ((lb + 1) >= ARRAY_SIZE(zdet_dnl_table)) {
  749. z_interp = (s64) ((flag_se_diff) ? (zdet_dnl_table[lb].diff_corr_mohms) :
  750. (zdet_dnl_table[lb].se_corr_mohms));
  751. goto apply_interpolated_bias;
  752. }
  753. /* Set up interpolation */
  754. lb_to_ub = OHMS_TO_MILLIOHMS *
  755. (s64) (u64) ((zdet_dnl_table[lb + 1].base_val_ohms -
  756. zdet_dnl_table[lb].base_val_ohms));
  757. z_to_ub = (OHMS_TO_MILLIOHMS *
  758. ((s64) (u64) (zdet_dnl_table[lb + 1].base_val_ohms))) - z_meas_bias_removed;
  759. lb_to_z = z_meas_bias_removed - (OHMS_TO_MILLIOHMS *
  760. ((s64) (u64) (zdet_dnl_table[lb].base_val_ohms)));
  761. lb_corr = (s64) ((flag_se_diff) ? (zdet_dnl_table[lb].diff_corr_mohms) :
  762. (zdet_dnl_table[lb].se_corr_mohms));
  763. ub_corr = (s64) ((flag_se_diff) ? (zdet_dnl_table[lb + 1].diff_corr_mohms) :
  764. (zdet_dnl_table[lb + 1].se_corr_mohms));
  765. /* Linear interpolation */
  766. z_interp = (lb_corr * z_to_ub + ub_corr * lb_to_z) / lb_to_ub;
  767. apply_interpolated_bias:
  768. /* Subtract interpolated bias to correct error */
  769. if (z_interp < z_val_slope_corrected)
  770. *z = (u32) (s32) (z_val_slope_corrected - z_interp);
  771. }
  772. static int get_lb_zdet_base_val_index(uint32_t z_val)
  773. {
  774. int i;
  775. /* Find the lower bound index, whose base value is the smallest value that is still higher
  776. * than the load
  777. */
  778. for (i = 1; i < ARRAY_SIZE(zdet_dnl_table); i++) {
  779. if (z_val < (OHMS_TO_MILLIOHMS * (u32) zdet_dnl_table[i].base_val_ohms))
  780. return i - 1;
  781. }
  782. /* Return the last index if the load is larger than all base values */
  783. return ARRAY_SIZE(zdet_dnl_table) - 1;
  784. }
  785. static void apply_zdet_correction(uint32_t *z, int flag_se_diff, u32 se_slope_factor_times_1000,
  786. u32 diff_slope_factor_times_1000)
  787. {
  788. s64 z_val_slope_corrected = 0, slope_corr = 0;
  789. uint32_t z_meas_bias_removed = 0;
  790. int lb;
  791. /* Apply slope correction */
  792. slope_corr = (s64) ((flag_se_diff) ? diff_slope_factor_times_1000 :
  793. se_slope_factor_times_1000);
  794. z_val_slope_corrected = ((s64) (u64) *z) * (FLOAT_TO_FIXED_XTALK) * slope_corr /
  795. SLOPE_FACTOR_SCALER / (FLOAT_TO_FIXED_XTALK);
  796. /* Interpolate correction term to bias out and apply correction */
  797. z_meas_bias_removed = (flag_se_diff) ? *z - DIFF_SLOPE_MEAS_BIAS : *z - SE_SLOPE_MEAS_BIAS;
  798. lb = get_lb_zdet_base_val_index(z_meas_bias_removed);
  799. interpolate_zdet_val(z, (s64) (u64) z_meas_bias_removed, z_val_slope_corrected, lb,
  800. flag_se_diff);
  801. }
  802. static void get_r_common_gnd(struct wcd939x_priv *wcd939x, u32 r_gnd_res_tot_mohms,
  803. u32 r_gnd_int_fet_mohms, u32 r_gnd_par_route1_mohms)
  804. {
  805. u32 r_common_gnd_mohms = 0, r_accum = 0, r_avg = 0;
  806. size_t i;
  807. struct wcd939x_pdata *pdata = dev_get_platdata(wcd939x->dev);
  808. size_t index = pdata->usbcss_hs.gnd.r_cm_gnd_buffer.write_index;
  809. r_common_gnd_mohms = r_gnd_res_tot_mohms - r_gnd_int_fet_mohms - r_gnd_par_route1_mohms;
  810. /* Compute average from r_common_gnd buffer */
  811. for (i = 0; i < R_COMMON_GND_BUFFER_SIZE; i++)
  812. r_accum += pdata->usbcss_hs.gnd.r_cm_gnd_buffer.data[i];
  813. r_avg = r_accum / R_COMMON_GND_BUFFER_SIZE;
  814. /* If r_common_gnd_mohms is OOB, use the average of the buffer values instead */
  815. if (r_common_gnd_mohms > (r_avg + pdata->usbcss_hs.gnd.r_common_gnd_margin) ||
  816. (r_avg >= pdata->usbcss_hs.gnd.r_common_gnd_margin &&
  817. r_common_gnd_mohms < (r_avg - pdata->usbcss_hs.gnd.r_common_gnd_margin))) {
  818. dev_dbg(wcd939x->dev, "%s: %s %d %s %d %s\n", __func__,
  819. "The average of the r_common_gnd buffer,", r_avg,
  820. "mohms, is being used instead of the calculated r_common_gnd value of",
  821. r_common_gnd_mohms, "mohms");
  822. pdata->usbcss_hs.gnd.r_common_gnd_mohms = r_avg;
  823. return;
  824. }
  825. /* Otherwise, use the computed value and store it in the buffer, updating the write index */
  826. pdata->usbcss_hs.gnd.r_common_gnd_mohms = r_common_gnd_mohms;
  827. pdata->usbcss_hs.gnd.r_cm_gnd_buffer.data[index] = r_common_gnd_mohms;
  828. pdata->usbcss_hs.gnd.r_cm_gnd_buffer.write_index = (index + 1) % R_COMMON_GND_BUFFER_SIZE;
  829. dev_dbg(wcd939x->dev, "%s: %s %d %s\n", __func__, "The calculated r_common_gnd value,",
  830. r_common_gnd_mohms, "mohms, is being used");
  831. }
  832. struct usbcss_hs_attr {
  833. struct wcd939x_priv *priv;
  834. struct kobj_attribute attr;
  835. int index;
  836. };
  837. static char *usbcss_sysfs_files[] = {
  838. "rdson_3p6v",
  839. "rdson_6v",
  840. "r1_l",
  841. "r1_r",
  842. "r3_l",
  843. "r3_r",
  844. "r4_sbu1",
  845. "r4_sbu2",
  846. "r5_sbu1",
  847. "r5_sbu2",
  848. "r6_sbu1",
  849. "r6_sbu2",
  850. "r7_sbu1",
  851. "r7_sbu2",
  852. "r_common_gnd_offset",
  853. "rcom_margin",
  854. "se_slope_factor_times_1000",
  855. "diff_slope_factor_times_1000",
  856. "lin_k_aud",
  857. "xtalk_config",
  858. };
  859. static ssize_t usbcss_sysfs_store(struct kobject *kobj, struct kobj_attribute *attr,
  860. const char *buf, size_t count)
  861. {
  862. struct usbcss_hs_attr *usbc_attr;
  863. struct wcd939x_priv *wcd939x;
  864. struct wcd939x_pdata *pdata;
  865. struct wcd939x_usbcss_hs_params *usbcss_hs;
  866. long val;
  867. int rc;
  868. u32 aud_tap = 0;
  869. bool update_xtalk = false, update_linearizer = false;
  870. usbc_attr = container_of(attr, struct usbcss_hs_attr, attr);
  871. if (!usbc_attr)
  872. return -EINVAL;
  873. wcd939x = usbc_attr->priv;
  874. if (!wcd939x)
  875. return -EINVAL;
  876. pdata = dev_get_platdata(wcd939x->dev);
  877. if (!pdata)
  878. return -EINVAL;
  879. usbcss_hs = &pdata->usbcss_hs;
  880. rc = kstrtol(buf, 0, &val);
  881. if (rc)
  882. return rc;
  883. if (strcmp(attr->attr.name, "rdson_3p6v") == 0) {
  884. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  885. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  886. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  887. return count;
  888. }
  889. usbcss_hs->gnd.rdson_3p6v_mohms = val;
  890. usbcss_hs->gnd.gnd_ext_fet_delta_mohms = (s32) (usbcss_hs->gnd.rdson_3p6v_mohms -
  891. usbcss_hs->gnd.rdson_mohms);
  892. update_linearizer = usbcss_hs->xtalk.xtalk_config == XTALK_ANALOG;
  893. } else if (strcmp(attr->attr.name, "rdson_6v") == 0) {
  894. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  895. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  896. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  897. return count;
  898. }
  899. usbcss_hs->gnd.rdson_mohms = val;
  900. update_linearizer = usbcss_hs->xtalk.xtalk_config == XTALK_ANALOG;
  901. } else if (strcmp(attr->attr.name, "r1_l") == 0) {
  902. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  903. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  904. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  905. return count;
  906. }
  907. usbcss_hs->aud.l.r1 = val;
  908. update_xtalk = true;
  909. } else if (strcmp(attr->attr.name, "r1_r") == 0) {
  910. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  911. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  912. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  913. return count;
  914. }
  915. usbcss_hs->aud.r.r1 = val;
  916. update_xtalk = true;
  917. } else if (strcmp(attr->attr.name, "r3_l") == 0) {
  918. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  919. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  920. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  921. return count;
  922. }
  923. usbcss_hs->aud.l.r3 = val;
  924. update_linearizer = true;
  925. } else if (strcmp(attr->attr.name, "r3_r") == 0) {
  926. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  927. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  928. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  929. return count;
  930. }
  931. usbcss_hs->aud.r.r3 = val;
  932. update_linearizer = true;
  933. } else if (strcmp(attr->attr.name, "r4_sbu1") == 0) {
  934. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  935. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  936. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  937. return count;
  938. }
  939. usbcss_hs->gnd.sbu1.r4 = val;
  940. update_xtalk = true;
  941. update_linearizer = true;
  942. } else if (strcmp(attr->attr.name, "r4_sbu2") == 0) {
  943. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  944. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  945. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  946. return count;
  947. }
  948. usbcss_hs->gnd.sbu2.r4 = val;
  949. update_xtalk = true;
  950. update_linearizer = true;
  951. } else if (strcmp(attr->attr.name, "r5_sbu1") == 0) {
  952. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  953. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  954. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  955. return count;
  956. }
  957. usbcss_hs->gnd.sbu1.r5 = val;
  958. switch (usbcss_hs->xtalk.xtalk_config) {
  959. case XTALK_ANALOG:
  960. update_xtalk = true;
  961. update_linearizer = true;
  962. break;
  963. case XTALK_DIGITAL:
  964. fallthrough;
  965. case XTALK_NONE:
  966. fallthrough;
  967. default:
  968. return count;
  969. }
  970. } else if (strcmp(attr->attr.name, "r5_sbu2") == 0) {
  971. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  972. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  973. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  974. return count;
  975. }
  976. usbcss_hs->gnd.sbu2.r5 = val;
  977. switch (usbcss_hs->xtalk.xtalk_config) {
  978. case XTALK_ANALOG:
  979. update_xtalk = true;
  980. update_linearizer = true;
  981. break;
  982. case XTALK_DIGITAL:
  983. fallthrough;
  984. case XTALK_NONE:
  985. fallthrough;
  986. default:
  987. return count;
  988. }
  989. } else if (strcmp(attr->attr.name, "r6_sbu1") == 0) {
  990. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  991. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  992. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  993. return count;
  994. }
  995. usbcss_hs->gnd.sbu1.r6 = val;
  996. switch (usbcss_hs->xtalk.xtalk_config) {
  997. case XTALK_DIGITAL:
  998. update_xtalk = true;
  999. update_linearizer = true;
  1000. break;
  1001. case XTALK_ANALOG:
  1002. fallthrough;
  1003. case XTALK_NONE:
  1004. fallthrough;
  1005. default:
  1006. return count;
  1007. }
  1008. } else if (strcmp(attr->attr.name, "r6_sbu2") == 0) {
  1009. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1010. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1011. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1012. return count;
  1013. }
  1014. usbcss_hs->gnd.sbu2.r6 = val;
  1015. switch (usbcss_hs->xtalk.xtalk_config) {
  1016. case XTALK_DIGITAL:
  1017. update_xtalk = true;
  1018. update_linearizer = true;
  1019. break;
  1020. case XTALK_ANALOG:
  1021. fallthrough;
  1022. case XTALK_NONE:
  1023. fallthrough;
  1024. default:
  1025. return count;
  1026. }
  1027. } else if (strcmp(attr->attr.name, "r7_sbu1") == 0) {
  1028. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1029. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1030. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1031. return count;
  1032. }
  1033. usbcss_hs->gnd.sbu1.r7 = val;
  1034. switch (usbcss_hs->xtalk.xtalk_config) {
  1035. case XTALK_DIGITAL:
  1036. update_xtalk = true;
  1037. update_linearizer = true;
  1038. break;
  1039. case XTALK_ANALOG:
  1040. fallthrough;
  1041. case XTALK_NONE:
  1042. fallthrough;
  1043. default:
  1044. return count;
  1045. }
  1046. } else if (strcmp(attr->attr.name, "r7_sbu2") == 0) {
  1047. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1048. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1049. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1050. return count;
  1051. }
  1052. usbcss_hs->gnd.sbu2.r7 = val;
  1053. switch (usbcss_hs->xtalk.xtalk_config) {
  1054. case XTALK_DIGITAL:
  1055. update_xtalk = true;
  1056. update_linearizer = true;
  1057. break;
  1058. case XTALK_ANALOG:
  1059. fallthrough;
  1060. case XTALK_NONE:
  1061. fallthrough;
  1062. default:
  1063. return count;
  1064. }
  1065. } else if (strcmp(attr->attr.name, "r_common_gnd_offset") == 0) {
  1066. if (val < -MAX_USBCSS_HS_IMPEDANCE_MOHMS || val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1067. dev_err(wcd939x->dev, "%s: Value %d out of bounds. Min: %d, Max: %d\n",
  1068. __func__, val, -MAX_USBCSS_HS_IMPEDANCE_MOHMS,
  1069. MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1070. return count;
  1071. }
  1072. usbcss_hs->gnd.r_common_gnd_offset = val;
  1073. update_xtalk = true;
  1074. } else if (strcmp(attr->attr.name, "rcom_margin") == 0) {
  1075. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1076. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1077. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1078. return count;
  1079. }
  1080. usbcss_hs->gnd.r_common_gnd_margin = val;
  1081. } else if (strcmp(attr->attr.name, "se_slope_factor_times_1000") == 0) {
  1082. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1083. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1084. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1085. return count;
  1086. }
  1087. usbcss_hs->se_slope_factor_times_1000 = val;
  1088. } else if (strcmp(attr->attr.name, "diff_slope_factor_times_1000") == 0) {
  1089. if (val > MAX_DIFF_SLOPE_FACTOR || val < MIN_DIFF_SLOPE_FACTOR) {
  1090. dev_err(wcd939x->dev, "%s: Value %d out of range of %d to %d\n",
  1091. __func__, val, MIN_DIFF_SLOPE_FACTOR, MAX_DIFF_SLOPE_FACTOR);
  1092. return count;
  1093. }
  1094. usbcss_hs->diff_slope_factor_times_1000 = val;
  1095. } else if (strcmp(attr->attr.name, "lin_k_aud") == 0) {
  1096. if (val < MIN_K_TIMES_100 || val > MAX_K_TIMES_100) {
  1097. dev_err(wcd939x->dev, "%s: Value %d out of bounds. Min: %d, Max: %d\n",
  1098. __func__, val, MIN_K_TIMES_100, MAX_K_TIMES_100);
  1099. return count;
  1100. }
  1101. usbcss_hs->aud.k_aud_times_100 = val;
  1102. update_linearizer = true;
  1103. } else if (strcmp(attr->attr.name, "xtalk_config") == 0) {
  1104. pdata->usbcss_hs.xtalk.xtalk_config = val;
  1105. update_xtalk = true;
  1106. switch (val) {
  1107. case XTALK_NONE:
  1108. usbcss_hs->xtalk.scale_l = MAX_XTALK_SCALE;
  1109. usbcss_hs->xtalk.scale_r = MAX_XTALK_SCALE;
  1110. usbcss_hs->xtalk.alpha_l = MIN_XTALK_ALPHA;
  1111. usbcss_hs->xtalk.alpha_r = MIN_XTALK_ALPHA;
  1112. break;
  1113. case XTALK_DIGITAL:
  1114. update_linearizer = true;
  1115. break;
  1116. case XTALK_ANALOG:
  1117. update_linearizer = true;
  1118. break;
  1119. default:
  1120. return count;
  1121. }
  1122. }
  1123. /* Update parastics */
  1124. switch (pdata->usbcss_hs.xtalk.xtalk_config) {
  1125. case XTALK_NONE:
  1126. fallthrough;
  1127. case XTALK_DIGITAL:
  1128. usbcss_hs->gnd.sbu1.r_gnd_par_route1_mohms = usbcss_hs->gnd.sbu1.r7;
  1129. usbcss_hs->gnd.sbu2.r_gnd_par_route1_mohms = usbcss_hs->gnd.sbu2.r7;
  1130. usbcss_hs->gnd.sbu1.r_gnd_par_route2_mohms = usbcss_hs->gnd.sbu1.r6 +
  1131. usbcss_hs->gnd.sbu1.r4;
  1132. usbcss_hs->gnd.sbu2.r_gnd_par_route2_mohms = usbcss_hs->gnd.sbu2.r6 +
  1133. usbcss_hs->gnd.sbu2.r4;
  1134. break;
  1135. case XTALK_ANALOG:
  1136. usbcss_hs->gnd.sbu1.r_gnd_par_route1_mohms = usbcss_hs->gnd.sbu1.r5 +
  1137. usbcss_hs->gnd.sbu1.r4;
  1138. usbcss_hs->gnd.sbu2.r_gnd_par_route1_mohms = usbcss_hs->gnd.sbu2.r5 +
  1139. usbcss_hs->gnd.sbu2.r4;
  1140. usbcss_hs->gnd.sbu1.r_gnd_par_route2_mohms = 1;
  1141. usbcss_hs->gnd.sbu2.r_gnd_par_route2_mohms = 1;
  1142. break;
  1143. default:
  1144. return count;
  1145. }
  1146. /* Update parastics total */
  1147. usbcss_hs->gnd.sbu1.r_gnd_par_tot_mohms = usbcss_hs->gnd.sbu1.r_gnd_par_route1_mohms +
  1148. usbcss_hs->gnd.sbu1.r_gnd_par_route2_mohms;
  1149. usbcss_hs->gnd.sbu2.r_gnd_par_tot_mohms = usbcss_hs->gnd.sbu2.r_gnd_par_route1_mohms +
  1150. usbcss_hs->gnd.sbu2.r_gnd_par_route2_mohms;
  1151. if (update_xtalk) {
  1152. /* Apply r_common_gnd offset */
  1153. usbcss_hs->gnd.r_common_gnd_mohms = (usbcss_hs->gnd.r_common_gnd_offset >= 0) ?
  1154. usbcss_hs->gnd.r_common_gnd_mohms +
  1155. (u32) usbcss_hs->gnd.r_common_gnd_offset :
  1156. usbcss_hs->gnd.r_common_gnd_mohms -
  1157. (u32) (-1 * usbcss_hs->gnd.r_common_gnd_offset);
  1158. /* Compute and store xtalk values */
  1159. update_xtalk_scale_and_alpha(wcd939x);
  1160. /* Revert r_common_gnd offset */
  1161. usbcss_hs->gnd.r_common_gnd_mohms = (usbcss_hs->gnd.r_common_gnd_offset >= 0) ?
  1162. usbcss_hs->gnd.r_common_gnd_mohms -
  1163. (u32) usbcss_hs->gnd.r_common_gnd_offset :
  1164. usbcss_hs->gnd.r_common_gnd_mohms +
  1165. (u32) (-1 * usbcss_hs->gnd.r_common_gnd_offset);
  1166. /* Apply xtalk scale and alpha values */
  1167. regmap_update_bits(wcd939x->regmap, WCD939X_HPHL_RX_PATH_SEC0, 0x1F,
  1168. pdata->usbcss_hs.xtalk.scale_l);
  1169. regmap_update_bits(wcd939x->regmap, WCD939X_HPHL_RX_PATH_SEC1, 0xFF,
  1170. pdata->usbcss_hs.xtalk.alpha_l);
  1171. regmap_update_bits(wcd939x->regmap, WCD939X_HPHL_RX_PATH_SEC0 +
  1172. XTALK_CH_REG_ADDR_DELTA, 0x1F,
  1173. pdata->usbcss_hs.xtalk.scale_r);
  1174. regmap_update_bits(wcd939x->regmap, WCD939X_HPHL_RX_PATH_SEC1 +
  1175. XTALK_CH_REG_ADDR_DELTA, 0xFF,
  1176. pdata->usbcss_hs.xtalk.alpha_r);
  1177. dev_err(wcd939x->dev, "%s: Updated xtalk thru sysfs\n",
  1178. __func__);
  1179. dev_dbg(wcd939x->dev, "%s: Left-channel: Xtalk scale is 0x%x and alpha is 0x%x\n",
  1180. __func__, pdata->usbcss_hs.xtalk.scale_l, pdata->usbcss_hs.xtalk.alpha_l);
  1181. dev_dbg(wcd939x->dev, "%s: Right-channel: Xtalk scale is 0x%x and alpha is 0x%x\n",
  1182. __func__, pdata->usbcss_hs.xtalk.scale_r, pdata->usbcss_hs.xtalk.alpha_r);
  1183. }
  1184. if (update_linearizer) {
  1185. get_linearizer_taps(pdata, &aud_tap);
  1186. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1187. wcd_usbss_set_linearizer_sw_tap(aud_tap, LINEARIZER_DEFAULT_TAP);
  1188. #endif
  1189. dev_err(wcd939x->dev, "%s: Updated linearizer thru sysfs\n",
  1190. __func__);
  1191. dev_dbg(wcd939x->dev, "%s: Linearizer aud_tap is 0x%x\n",
  1192. __func__, aud_tap);
  1193. }
  1194. return count;
  1195. }
  1196. static ssize_t usbcss_sysfs_show(struct kobject *kobj,
  1197. struct kobj_attribute *attr, char *buf)
  1198. {
  1199. struct usbcss_hs_attr *usbc_attr;
  1200. struct wcd939x_priv *wcd939x;
  1201. struct wcd939x_pdata *pdata;
  1202. usbc_attr = container_of(attr, struct usbcss_hs_attr, attr);
  1203. wcd939x = usbc_attr->priv;
  1204. pdata = dev_get_platdata(wcd939x->dev);
  1205. if (strcmp(attr->attr.name, "rdson_3p6v") == 0)
  1206. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.rdson_3p6v_mohms);
  1207. else if (strcmp(attr->attr.name, "rdson_6v") == 0)
  1208. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.rdson_mohms);
  1209. else if (strcmp(attr->attr.name, "r1_l") == 0)
  1210. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.l.r1);
  1211. else if (strcmp(attr->attr.name, "r1_r") == 0)
  1212. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.r.r1);
  1213. else if (strcmp(attr->attr.name, "r3_l") == 0)
  1214. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.l.r3);
  1215. else if (strcmp(attr->attr.name, "r3_r") == 0)
  1216. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.r.r3);
  1217. else if (strcmp(attr->attr.name, "r4_sbu1") == 0)
  1218. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu1.r4);
  1219. else if (strcmp(attr->attr.name, "r4_sbu2") == 0)
  1220. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu2.r4);
  1221. else if (strcmp(attr->attr.name, "r5_sbu1") == 0)
  1222. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu1.r5);
  1223. else if (strcmp(attr->attr.name, "r5_sbu2") == 0)
  1224. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu2.r5);
  1225. else if (strcmp(attr->attr.name, "r6_sbu1") == 0)
  1226. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu1.r6);
  1227. else if (strcmp(attr->attr.name, "r6_sbu2") == 0)
  1228. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu2.r6);
  1229. else if (strcmp(attr->attr.name, "r7_sbu1") == 0)
  1230. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu1.r7);
  1231. else if (strcmp(attr->attr.name, "r7_sbu2") == 0)
  1232. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu2.r7);
  1233. else if (strcmp(attr->attr.name, "r_common_gnd_offset") == 0)
  1234. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.r_common_gnd_offset);
  1235. else if (strcmp(attr->attr.name, "rcom_margin") == 0)
  1236. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.r_common_gnd_margin);
  1237. else if (strcmp(attr->attr.name, "se_slope_factor_times_1000") == 0)
  1238. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.se_slope_factor_times_1000);
  1239. else if (strcmp(attr->attr.name, "diff_slope_factor_times_1000") == 0)
  1240. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.diff_slope_factor_times_1000);
  1241. else if (strcmp(attr->attr.name, "lin_k_aud") == 0)
  1242. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.k_aud_times_100);
  1243. else if (strcmp(attr->attr.name, "xtalk_config") == 0)
  1244. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.xtalk.xtalk_config);
  1245. return 0;
  1246. }
  1247. static int create_sysfs_entry_file(struct wcd939x_priv *wcd939x, char *name, int mode,
  1248. int index, struct kobject *parent)
  1249. {
  1250. struct usbcss_hs_attr *usbc_attr;
  1251. char *name_copy;
  1252. usbc_attr = devm_kmalloc(wcd939x->dev, sizeof(*usbc_attr), GFP_KERNEL);
  1253. if (!usbc_attr)
  1254. return -ENOMEM;
  1255. name_copy = devm_kstrdup(wcd939x->dev, name, GFP_KERNEL);
  1256. if (!name_copy)
  1257. return -ENOMEM;
  1258. usbc_attr->priv = wcd939x;
  1259. usbc_attr->index = index;
  1260. usbc_attr->attr.attr.name = name_copy;
  1261. usbc_attr->attr.attr.mode = mode;
  1262. usbc_attr->attr.show = usbcss_sysfs_show;
  1263. usbc_attr->attr.store = usbcss_sysfs_store;
  1264. sysfs_attr_init(&usbc_attr->attr.attr);
  1265. return sysfs_create_file(parent, &usbc_attr->attr.attr);
  1266. }
  1267. static int usbcss_hs_sysfs_init(struct wcd939x_priv *wcd939x)
  1268. {
  1269. int rc = 0;
  1270. int i = 0;
  1271. struct kobject *kobj = NULL;
  1272. if (!wcd939x || !wcd939x->dev) {
  1273. pr_err("%s: Invalid wcd939x private data.\n", __func__);
  1274. return -EINVAL;
  1275. }
  1276. kobj = kobject_create_and_add("usbcss_hs", kernel_kobj);
  1277. if (!kobj) {
  1278. dev_err(wcd939x->dev, "%s: Could not create the USBC-SS HS kobj.\n", __func__);
  1279. return -ENOMEM;
  1280. }
  1281. for (i = 0; i < ARRAY_SIZE(usbcss_sysfs_files); i++) {
  1282. rc = create_sysfs_entry_file(wcd939x, usbcss_sysfs_files[i],
  1283. 0644, i, kobj);
  1284. }
  1285. return 0;
  1286. }
  1287. static void wcd939x_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl, uint32_t *zr)
  1288. {
  1289. struct snd_soc_component *component = mbhc->component;
  1290. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  1291. struct wcd939x_pdata *pdata = dev_get_platdata(wcd939x->dev);
  1292. s16 reg0, reg1, reg2, reg3, reg4;
  1293. uint32_t zdiff_val = 0, r_gnd_int_fet_mohms = 0, rl_eff_l_mohms = 0, rl_eff_r_mohms = 0;
  1294. uint32_t r_gnd_ext_fet_mohms = 0, r_aud_ext_fet_mohms = 0, r_gnd_res_tot_mohms = 0;
  1295. uint32_t r_gnd_par_tot_mohms = 0, r_gnd_par_route1_mohms = 0;
  1296. uint32_t aud_tap = LINEARIZER_DEFAULT_TAP, zdiff_counter = 0, zdiff_sum = 0;
  1297. uint32_t *zdiff = &zdiff_val;
  1298. s32 z_L_R_delta_mohms = 0;
  1299. int32_t z1L, z1R, z1Ls, z1Diff;
  1300. int zMono, z_diff1, z_diff2;
  1301. size_t i;
  1302. bool is_fsm_disable = false, calculate_lin_aud_tap = false, gnd_ext_fet_updated = false;
  1303. struct wcd939x_mbhc_zdet_param zdet_param = {4, 0, 6, 0x18, 0x60, 0x78};
  1304. struct wcd939x_mbhc_zdet_param *zdet_param_ptr = &zdet_param;
  1305. s16 d1[] = {0, 30, 30, 6};
  1306. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1307. uint32_t cached_regs[4][2] = {{WCD_USBSS_EXT_LIN_EN, 0}, {WCD_USBSS_EXT_SW_CTRL_1, 0},
  1308. {WCD_USBSS_MG1_BIAS, 0}, {WCD_USBSS_MG2_BIAS, 0}};
  1309. uint32_t l_3_6V_regs[4][2] = {{WCD_USBSS_EXT_LIN_EN, 0x00}, {WCD_USBSS_EXT_SW_CTRL_1, 0x00},
  1310. {WCD_USBSS_MG1_BIAS, 0x0E}, {WCD_USBSS_MG2_BIAS, 0x0E}};
  1311. uint32_t diff_regs[2][2] = {{WCD_USBSS_EXT_LIN_EN, 0x00}, {WCD_USBSS_EXT_SW_CTRL_1, 0x00}};
  1312. #endif
  1313. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  1314. /* Turn on RX supplies */
  1315. if (wcd939x->version == WCD939X_VERSION_2_0) {
  1316. /* Start up Buck/Flyback, Enable RX bias, Use MBHC RCO for MBHC Zdet, Enable Vneg */
  1317. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x4E, 0x4E);
  1318. /* Wait 100us for settling */
  1319. usleep_range(100, 110);
  1320. /* Enable VNEGDAC_LDO */
  1321. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x10, 0x10);
  1322. /* Wait 100us for settling */
  1323. usleep_range(100, 110);
  1324. /* Keep PA left/right channels disabled */
  1325. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x01, 0x01);
  1326. /* Enable VPOS */
  1327. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x20, 0x20);
  1328. /* Wait 500us for settling */
  1329. usleep_range(500, 510);
  1330. }
  1331. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1332. /* Cache relevant USB-SS registers */
  1333. wcd_usbss_register_update(cached_regs, WCD_USBSS_READ, ARRAY_SIZE(cached_regs));
  1334. /* Disable 2k pulldown on MG for improved measurement */
  1335. wcd_usbss_register_update(l_3_6V_regs, WCD_USBSS_WRITE, ARRAY_SIZE(l_3_6V_regs));
  1336. #endif
  1337. /* Store register values */
  1338. reg0 = snd_soc_component_read(component, WCD939X_MBHC_BTN5);
  1339. reg1 = snd_soc_component_read(component, WCD939X_MBHC_BTN6);
  1340. reg2 = snd_soc_component_read(component, WCD939X_MBHC_BTN7);
  1341. reg3 = snd_soc_component_read(component, WCD939X_CTL_CLK);
  1342. reg4 = snd_soc_component_read(component, WCD939X_ZDET_ANA_CTL);
  1343. /* Disable the detection FSM */
  1344. if (snd_soc_component_read(component, WCD939X_MBHC_ELECT) & 0x80) {
  1345. is_fsm_disable = true;
  1346. regmap_update_bits(wcd939x->regmap,
  1347. WCD939X_MBHC_ELECT, 0x80, 0x00);
  1348. }
  1349. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  1350. if (mbhc->hphl_swh)
  1351. regmap_update_bits(wcd939x->regmap,
  1352. WCD939X_MBHC_MECH, 0x80, 0x00);
  1353. /* Turn off 100k pull down on HPHL */
  1354. regmap_update_bits(wcd939x->regmap,
  1355. WCD939X_MBHC_MECH, 0x01, 0x00);
  1356. /* Disable surge protection before impedance detection.
  1357. * This is done to give correct value for high impedance.
  1358. */
  1359. regmap_update_bits(wcd939x->regmap,
  1360. WCD939X_HPHLR_SURGE_EN, 0xC0, 0x00);
  1361. /* 1ms delay needed after disable surge protection */
  1362. usleep_range(1000, 1010);
  1363. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1364. /* Disable sense switch and MIC for USB-C analog platforms */
  1365. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1366. wcd_usbss_set_switch_settings_enable(SENSE_SWITCHES, USBSS_SWITCH_DISABLE);
  1367. wcd_usbss_set_switch_settings_enable(MIC_SWITCHES, USBSS_SWITCH_DISABLE);
  1368. }
  1369. #endif
  1370. /* L-channel impedance */
  1371. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  1372. if ((z1L == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1L > WCD939X_ZDET_VAL_100K)) {
  1373. *zl = WCD939X_ZDET_FLOATING_IMPEDANCE;
  1374. } else {
  1375. *zl = z1L;
  1376. wcd939x_wcd_mbhc_qfuse_cal(component, zl, 0);
  1377. dev_dbg(component->dev, "%s: Calibrated left SE measurement is %d(mohms)\n",
  1378. __func__, *zl);
  1379. apply_zdet_correction(zl, ZDET_SE, pdata->usbcss_hs.se_slope_factor_times_1000,
  1380. pdata->usbcss_hs.diff_slope_factor_times_1000);
  1381. }
  1382. pdata->usbcss_hs.aud.l.zval = *zl;
  1383. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1384. dev_dbg(component->dev,
  1385. "%s: Calibrated and adjusted left SE measurement is %d(mohms)\n", __func__,
  1386. *zl);
  1387. } else {
  1388. dev_dbg(component->dev, "%s: impedance on HPH_L = %d(mohms)\n",
  1389. __func__, *zl);
  1390. }
  1391. /* R-channel impedance */
  1392. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
  1393. if ((z1R == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1R > WCD939X_ZDET_VAL_100K)) {
  1394. *zr = WCD939X_ZDET_FLOATING_IMPEDANCE;
  1395. } else {
  1396. *zr = z1R;
  1397. wcd939x_wcd_mbhc_qfuse_cal(component, zr, 4);
  1398. dev_dbg(component->dev, "%s: Calibrated right SE measurement is %d(mohms)\n",
  1399. __func__, *zr);
  1400. apply_zdet_correction(zr, ZDET_SE, pdata->usbcss_hs.se_slope_factor_times_1000,
  1401. pdata->usbcss_hs.diff_slope_factor_times_1000);
  1402. }
  1403. pdata->usbcss_hs.aud.r.zval = *zr;
  1404. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1405. dev_dbg(component->dev,
  1406. "%s: Calibrated and adjusted right SE measurement is %d(mohms)\n", __func__,
  1407. *zr);
  1408. } else {
  1409. dev_dbg(component->dev, "%s: impedance on HPH_R = %d(mohms)\n",
  1410. __func__, *zr);
  1411. /* Convert from mohms to ohms (rounded) */
  1412. *zl = (*zl + OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  1413. *zr = (*zr + OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  1414. goto mono_stereo_detection;
  1415. }
  1416. /* Differential measurement L to R */
  1417. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1418. /* Disable AGND switch */
  1419. wcd_usbss_set_switch_settings_enable(AGND_SWITCHES, USBSS_SWITCH_DISABLE);
  1420. wcd_usbss_register_update(diff_regs, WCD_USBSS_WRITE, ARRAY_SIZE(diff_regs));
  1421. #endif
  1422. /* Enable HPHR NCLAMP */
  1423. regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_MISC1, 0x08, 0x08);
  1424. /* Wait 3ms for settling */
  1425. usleep_range(3000, 3010);
  1426. /* Differential impedance */
  1427. for (i = 0; i < NUM_DIFF_MEAS; i++) {
  1428. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1Diff, NULL, d1);
  1429. if ((z1Diff == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
  1430. (z1Diff > WCD939X_ZDET_VAL_100K)) {
  1431. } else {
  1432. *zdiff = z1Diff;
  1433. wcd939x_wcd_mbhc_qfuse_cal(component, zdiff, 0);
  1434. dev_dbg(component->dev,
  1435. "%s: Calibrated differential measurement %d is %d(mohms)\n",
  1436. __func__, i + 1, *zdiff);
  1437. apply_zdet_correction(zdiff, ZDET_DIFF,
  1438. pdata->usbcss_hs.se_slope_factor_times_1000,
  1439. pdata->usbcss_hs.diff_slope_factor_times_1000);
  1440. zdiff_sum += *zdiff;
  1441. zdiff_counter++;
  1442. }
  1443. dev_dbg(component->dev,
  1444. "%s: Calibrated and adjusted differential measurement %d is %d(mohms)\n",
  1445. __func__, i + 1, *zdiff);
  1446. }
  1447. /* Take average of measurements */
  1448. if (zdiff_counter == 0)
  1449. *zdiff = WCD939X_ZDET_FLOATING_IMPEDANCE;
  1450. else
  1451. *zdiff = zdiff_sum / zdiff_counter;
  1452. /* Store the average of the measurements */
  1453. pdata->usbcss_hs.zdiffval = *zdiff;
  1454. dev_dbg(component->dev, "%s: %s %d(mohms)\n", __func__,
  1455. "Average of the calibrated and adjusted differential measurement(s) is", *zdiff);
  1456. /* Disable HPHR NCLAMP */
  1457. regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_MISC1, 0x08, 0x00);
  1458. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1459. /* Enable AGND switch */
  1460. wcd_usbss_set_switch_settings_enable(AGND_SWITCHES, USBSS_SWITCH_ENABLE);
  1461. /* Get ground internal resistance based on orientation */
  1462. if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
  1463. r_gnd_int_fet_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_int_fet_mohms;
  1464. r_gnd_par_route1_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_par_route1_mohms;
  1465. r_gnd_par_tot_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_par_tot_mohms;
  1466. } else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
  1467. r_gnd_int_fet_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_int_fet_mohms;
  1468. r_gnd_par_route1_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_par_route1_mohms;
  1469. r_gnd_par_tot_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_par_tot_mohms;
  1470. } else {
  1471. dev_dbg(component->dev, "%s: Invalid SBU switch orientation\n", __func__);
  1472. *zl = 0;
  1473. *zr = 0;
  1474. goto default_vals;
  1475. }
  1476. #endif
  1477. z_L_R_delta_mohms = *zl - *zr;
  1478. dev_dbg(component->dev, "%s: %s : %d mohms\n", __func__,
  1479. "The difference between the L and R SE measurements (L - R) is", z_L_R_delta_mohms);
  1480. /* Ground path resistance */
  1481. /* Use DTSI params for high zdet SE measurements */
  1482. if (pdata->usbcss_hs.aud.l.zval > ZDET_ACC_LMT_MOHMS ||
  1483. pdata->usbcss_hs.aud.r.zval > ZDET_ACC_LMT_MOHMS) {
  1484. r_gnd_res_tot_mohms = pdata->usbcss_hs.gnd.rdson_mohms + r_gnd_par_tot_mohms +
  1485. r_gnd_int_fet_mohms;
  1486. pdata->usbcss_hs.gnd.r_common_gnd_mohms = r_gnd_res_tot_mohms -
  1487. r_gnd_int_fet_mohms -
  1488. r_gnd_par_route1_mohms;
  1489. dev_dbg(component->dev, "%s: %s %d %s\n", __func__,
  1490. "The r_common_gnd value determined by DTSI parameters,",
  1491. pdata->usbcss_hs.gnd.r_common_gnd_mohms,
  1492. "mohms, is being used instead of calculating r_common_gnd");
  1493. calculate_lin_aud_tap = false;
  1494. } else {
  1495. r_gnd_res_tot_mohms = (*zl + *zr - *zdiff + pdata->usbcss_hs.aud.r_surge_mohms) / 2;
  1496. /* Offset to account for using 3.6V SE measurements */
  1497. r_gnd_res_tot_mohms = (pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms >= 0) ?
  1498. r_gnd_res_tot_mohms -
  1499. (u32) (s32) pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms :
  1500. r_gnd_res_tot_mohms +
  1501. (u32) (s32) (-1 * pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms);
  1502. /* Compute r_common_gnd */
  1503. get_r_common_gnd(wcd939x, r_gnd_res_tot_mohms, r_gnd_int_fet_mohms,
  1504. r_gnd_par_route1_mohms);
  1505. /* Re-calculate ground path resistance based on r_common_gnd */
  1506. r_gnd_res_tot_mohms = pdata->usbcss_hs.gnd.r_common_gnd_mohms +
  1507. r_gnd_int_fet_mohms + r_gnd_par_route1_mohms;
  1508. calculate_lin_aud_tap = true;
  1509. }
  1510. dev_dbg(component->dev, "%s: r_gnd_res_tot_mohms is : %d mohms\n", __func__,
  1511. r_gnd_res_tot_mohms);
  1512. /* Print r_common_gnd buffer */
  1513. for (i = 0; i < R_COMMON_GND_BUFFER_SIZE; i++) {
  1514. dev_dbg(component->dev, "%s: Element %d in r_common_gnd_buffer is : %d mohms\n",
  1515. __func__, i + 1, pdata->usbcss_hs.gnd.r_cm_gnd_buffer.data[i]);
  1516. }
  1517. /* Apply r_common_gnd offset */
  1518. pdata->usbcss_hs.gnd.r_common_gnd_mohms =
  1519. (pdata->usbcss_hs.gnd.r_common_gnd_offset >= 0) ?
  1520. pdata->usbcss_hs.gnd.r_common_gnd_mohms +
  1521. (u32) pdata->usbcss_hs.gnd.r_common_gnd_offset :
  1522. pdata->usbcss_hs.gnd.r_common_gnd_mohms -
  1523. (u32) (-1 * pdata->usbcss_hs.gnd.r_common_gnd_offset);
  1524. /* Ground external FET */
  1525. r_gnd_ext_fet_mohms = r_gnd_res_tot_mohms - r_gnd_par_tot_mohms - r_gnd_int_fet_mohms;
  1526. dev_dbg(component->dev, "%s: r_gnd_ext_fet_mohms is : %d mohms\n", __func__,
  1527. r_gnd_ext_fet_mohms);
  1528. /* Audio external FET */
  1529. r_aud_ext_fet_mohms = (pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms >= 0) ?
  1530. r_gnd_ext_fet_mohms + (u32) (s32) pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms :
  1531. r_gnd_ext_fet_mohms - (u32) (s32) (-1 * pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms);
  1532. dev_dbg(component->dev, "%s: r_aud_ext_fet_mohms is : %d mohms\n", __func__,
  1533. r_aud_ext_fet_mohms);
  1534. /* Compute effective load resistance */
  1535. rl_eff_l_mohms = *zl - pdata->usbcss_hs.aud.l.r_aud_int_fet_mohms - r_aud_ext_fet_mohms -
  1536. r_gnd_res_tot_mohms;
  1537. rl_eff_r_mohms = *zr - pdata->usbcss_hs.aud.r.r_aud_int_fet_mohms - r_aud_ext_fet_mohms -
  1538. r_gnd_res_tot_mohms;
  1539. /* Store z values */
  1540. *zl = (rl_eff_l_mohms - R_CONN_PAR_LOAD_POS_MOHMS - pdata->usbcss_hs.aud.l.r3 +
  1541. OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  1542. dev_dbg(component->dev, "%s: rload_l is : %d mohms\n", __func__,
  1543. rl_eff_l_mohms - R_CONN_PAR_LOAD_POS_MOHMS - pdata->usbcss_hs.aud.l.r3);
  1544. *zr = (rl_eff_r_mohms - R_CONN_PAR_LOAD_POS_MOHMS - pdata->usbcss_hs.aud.r.r3 +
  1545. OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  1546. dev_dbg(component->dev, "%s: rload_r is : %d mohms\n", __func__,
  1547. rl_eff_r_mohms - R_CONN_PAR_LOAD_POS_MOHMS - pdata->usbcss_hs.aud.r.r3);
  1548. /* Check bounds on effective load values and store the value */
  1549. if (rl_eff_l_mohms > MAX_RL_EFF_MOHMS)
  1550. rl_eff_l_mohms = MAX_RL_EFF_MOHMS;
  1551. else if (rl_eff_l_mohms < MIN_RL_EFF_MOHMS)
  1552. rl_eff_l_mohms = MIN_RL_EFF_MOHMS;
  1553. pdata->usbcss_hs.aud.l.r_load_eff_mohms = rl_eff_l_mohms;
  1554. if (rl_eff_r_mohms > MAX_RL_EFF_MOHMS)
  1555. rl_eff_r_mohms = MAX_RL_EFF_MOHMS;
  1556. else if (rl_eff_r_mohms < MIN_RL_EFF_MOHMS)
  1557. rl_eff_r_mohms = MIN_RL_EFF_MOHMS;
  1558. pdata->usbcss_hs.aud.r.r_load_eff_mohms = rl_eff_r_mohms;
  1559. /* Update FET values and resistances */
  1560. update_ext_fet_res(pdata, r_aud_ext_fet_mohms, r_gnd_ext_fet_mohms);
  1561. /* Update xtalk params */
  1562. /* For SE measurements greater than ZDET_SE_MAX_MOHMS, use default xtalk values */
  1563. if (pdata->usbcss_hs.aud.l.zval > ZDET_SE_MAX_MOHMS ||
  1564. pdata->usbcss_hs.aud.r.zval > ZDET_SE_MAX_MOHMS) {
  1565. pdata->usbcss_hs.xtalk.scale_l = MAX_XTALK_SCALE;
  1566. pdata->usbcss_hs.xtalk.scale_r = MAX_XTALK_SCALE;
  1567. pdata->usbcss_hs.xtalk.alpha_l = MIN_XTALK_ALPHA;
  1568. pdata->usbcss_hs.xtalk.alpha_r = MIN_XTALK_ALPHA;
  1569. dev_dbg(component->dev, "%s: %s %d, %s\n",
  1570. __func__, "The SE zdet measurement is greater than ZDET_SE_MAX_MOHMS,",
  1571. ZDET_SE_MAX_MOHMS,
  1572. "so the default xtalk scale and alpha values will be used");
  1573. } else {
  1574. update_xtalk_scale_and_alpha(wcd939x);
  1575. /* Compute updated linearizer tap */
  1576. if (calculate_lin_aud_tap) {
  1577. if (r_gnd_ext_fet_mohms < pdata->usbcss_hs.gnd.gnd_ext_fet_min_mohms) {
  1578. r_gnd_ext_fet_mohms = pdata->usbcss_hs.gnd.gnd_ext_fet_min_mohms;
  1579. gnd_ext_fet_updated = true;
  1580. }
  1581. if (r_gnd_ext_fet_mohms > GND_EXT_FET_MAX_MOHMS) {
  1582. r_gnd_ext_fet_mohms = GND_EXT_FET_MAX_MOHMS;
  1583. gnd_ext_fet_updated = true;
  1584. }
  1585. if (gnd_ext_fet_updated) {
  1586. dev_dbg(component->dev, "%s: %s %d mohms\n", __func__,
  1587. "Updated (for linearizer) r_gnd_ext_fet_mohms is :",
  1588. r_gnd_ext_fet_mohms);
  1589. /* Audio external FET */
  1590. r_aud_ext_fet_mohms =
  1591. (pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms >= 0) ?
  1592. r_gnd_ext_fet_mohms +
  1593. (u32) (s32) pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms :
  1594. r_gnd_ext_fet_mohms -
  1595. (u32) (s32) (-1 * pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms);
  1596. dev_dbg(component->dev, "%s: %s %d mohms\n", __func__,
  1597. "Updated (for linearizer) r_aud_ext_fet_mohms is :",
  1598. r_aud_ext_fet_mohms);
  1599. /* Update FET values and resistances */
  1600. update_ext_fet_res(pdata, r_aud_ext_fet_mohms, r_gnd_ext_fet_mohms);
  1601. }
  1602. get_linearizer_taps(pdata, &aud_tap);
  1603. }
  1604. }
  1605. /* Print xtalk params */
  1606. dev_dbg(component->dev, "%s: Left-channel: Xtalk scale is 0x%x and alpha is 0x%x\n",
  1607. __func__, pdata->usbcss_hs.xtalk.scale_l, pdata->usbcss_hs.xtalk.alpha_l);
  1608. dev_dbg(component->dev, "%s: Right-channel: Xtalk scale is 0x%x and alpha is 0x%x\n",
  1609. __func__, pdata->usbcss_hs.xtalk.scale_r, pdata->usbcss_hs.xtalk.alpha_r);
  1610. /* Revert r_common_gnd offset */
  1611. pdata->usbcss_hs.gnd.r_common_gnd_mohms = (pdata->usbcss_hs.gnd.r_common_gnd_offset >= 0) ?
  1612. pdata->usbcss_hs.gnd.r_common_gnd_mohms -
  1613. (u32) pdata->usbcss_hs.gnd.r_common_gnd_offset :
  1614. pdata->usbcss_hs.gnd.r_common_gnd_mohms +
  1615. (u32) (-1 * pdata->usbcss_hs.gnd.r_common_gnd_offset);
  1616. mono_stereo_detection:
  1617. /* Mono/stereo detection */
  1618. if ((*zl == WCD939X_ZDET_FLOATING_IMPEDANCE) && (*zr == WCD939X_ZDET_FLOATING_IMPEDANCE)) {
  1619. dev_dbg(component->dev,
  1620. "%s: plug type is invalid or extension cable\n",
  1621. __func__);
  1622. goto zdet_complete;
  1623. }
  1624. if ((*zl == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
  1625. (*zr == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
  1626. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  1627. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  1628. dev_dbg(component->dev,
  1629. "%s: Mono plug type with one ch floating or shorted to GND\n",
  1630. __func__);
  1631. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  1632. goto zdet_complete;
  1633. }
  1634. snd_soc_component_update_bits(component, WCD939X_R_ATEST, 0x02, 0x02);
  1635. snd_soc_component_update_bits(component, WCD939X_PA_CTL2, 0x40, 0x01);
  1636. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1Ls, NULL, d1);
  1637. snd_soc_component_update_bits(component, WCD939X_PA_CTL2, 0x40, 0x00);
  1638. snd_soc_component_update_bits(component, WCD939X_R_ATEST, 0x02, 0x00);
  1639. z1Ls /= 1000;
  1640. wcd939x_wcd_mbhc_qfuse_cal(component, &z1Ls, 0);
  1641. /* Parallel of left Z and 9 ohm pull down resistor */
  1642. zMono = ((*zl) * 9) / ((*zl) + 9);
  1643. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  1644. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  1645. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  1646. dev_dbg(component->dev, "%s: stereo plug type detected\n",
  1647. __func__);
  1648. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  1649. } else {
  1650. dev_dbg(component->dev, "%s: MONO plug type detected\n",
  1651. __func__);
  1652. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  1653. }
  1654. goto zdet_complete;
  1655. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1656. default_vals:
  1657. pdata->usbcss_hs.xtalk.scale_l = MAX_XTALK_SCALE;
  1658. pdata->usbcss_hs.xtalk.scale_r = MAX_XTALK_SCALE;
  1659. pdata->usbcss_hs.xtalk.alpha_l = MIN_XTALK_ALPHA;
  1660. pdata->usbcss_hs.xtalk.alpha_r = MIN_XTALK_ALPHA;
  1661. /* Print xtalk params */
  1662. dev_dbg(component->dev,
  1663. "%s: Left-channel: Xtalk scale is 0x%x and alpha is 0x%x\n", __func__,
  1664. pdata->usbcss_hs.xtalk.scale_l, pdata->usbcss_hs.xtalk.alpha_l);
  1665. dev_dbg(component->dev,
  1666. "%s: Right-channel: Xtalk scale is 0x%x and alpha is 0x%x\n", __func__,
  1667. pdata->usbcss_hs.xtalk.scale_r, pdata->usbcss_hs.xtalk.alpha_r);
  1668. #endif
  1669. zdet_complete:
  1670. /* Configure linearizer */
  1671. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1672. wcd_usbss_set_linearizer_sw_tap(aud_tap, LINEARIZER_DEFAULT_TAP);
  1673. #endif
  1674. /* Print linearizer values */
  1675. dev_dbg(component->dev, "%s: Linearizer aud_tap is 0x%x\n",
  1676. __func__, aud_tap);
  1677. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1678. /* Enable sense switch and MIC for USB-C analog platforms */
  1679. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1680. wcd_usbss_set_switch_settings_enable(SENSE_SWITCHES, USBSS_SWITCH_ENABLE);
  1681. wcd_usbss_set_switch_settings_enable(MIC_SWITCHES, USBSS_SWITCH_ENABLE);
  1682. }
  1683. #endif
  1684. /* Enable surge protection again after impedance detection for platforms other than USB-C
  1685. * analog platforms
  1686. */
  1687. if (!(mbhc->mbhc_cfg->enable_usbc_analog))
  1688. regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_EN, 0xC0, 0xC0);
  1689. snd_soc_component_write(component, WCD939X_MBHC_BTN5, reg0);
  1690. snd_soc_component_write(component, WCD939X_MBHC_BTN6, reg1);
  1691. snd_soc_component_write(component, WCD939X_MBHC_BTN7, reg2);
  1692. /* Turn on 100k pull down on HPHL */
  1693. regmap_update_bits(wcd939x->regmap,
  1694. WCD939X_MBHC_MECH, 0x01, 0x01);
  1695. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  1696. if (mbhc->hphl_swh)
  1697. regmap_update_bits(wcd939x->regmap,
  1698. WCD939X_MBHC_MECH, 0x80, 0x80);
  1699. snd_soc_component_write(component, WCD939X_ZDET_ANA_CTL, reg4);
  1700. snd_soc_component_write(component, WCD939X_CTL_CLK, reg3);
  1701. if (is_fsm_disable)
  1702. regmap_update_bits(wcd939x->regmap,
  1703. WCD939X_MBHC_ELECT, 0x80, 0x80);
  1704. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1705. wcd_usbss_register_update(cached_regs, WCD_USBSS_WRITE, ARRAY_SIZE(cached_regs));
  1706. #endif
  1707. /* Turn off RX supplies */
  1708. if (wcd939x->version == WCD939X_VERSION_2_0) {
  1709. /* Set VPOS to be controlled by RX */
  1710. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x20, 0x00);
  1711. /* Wait 500us for settling */
  1712. usleep_range(500, 510);
  1713. /* Set PA Left/Right channels and VNEGDAC_LDO to be controlled by RX */
  1714. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x11, 0x00);
  1715. /* Wait 100us for settling */
  1716. usleep_range(100, 110);
  1717. /* Set Vneg mode and enable to be controlled by RX */
  1718. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x06, 0x00);
  1719. /* Wait 100us for settling */
  1720. usleep_range(100, 110);
  1721. /* Set RX bias to be controlled by RX and set Buck/Flyback back to SWR Rx clock */
  1722. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x48, 0x00);
  1723. }
  1724. }
  1725. static void wcd939x_mbhc_gnd_det_ctrl(struct snd_soc_component *component,
  1726. bool enable)
  1727. {
  1728. if (enable) {
  1729. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1730. 0x02, 0x02);
  1731. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1732. 0x40, 0x40);
  1733. } else {
  1734. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1735. 0x40, 0x00);
  1736. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1737. 0x02, 0x00);
  1738. }
  1739. }
  1740. static void wcd939x_mbhc_hph_pull_down_ctrl(struct snd_soc_component *component,
  1741. bool enable)
  1742. {
  1743. if (enable) {
  1744. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1745. 0x40, 0x40);
  1746. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1747. 0x10, 0x10);
  1748. } else {
  1749. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1750. 0x40, 0x00);
  1751. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1752. 0x10, 0x00);
  1753. }
  1754. }
  1755. static void wcd939x_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  1756. {
  1757. struct snd_soc_component *component = mbhc->component;
  1758. if ((mbhc->moist_rref == R_OFF) ||
  1759. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  1760. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1761. 0x0C, R_OFF << 2);
  1762. return;
  1763. }
  1764. /* Do not enable moisture detection if jack type is NC */
  1765. if (!mbhc->hphl_swh) {
  1766. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  1767. __func__);
  1768. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1769. 0x0C, R_OFF << 2);
  1770. return;
  1771. }
  1772. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1773. 0x0C, mbhc->moist_rref << 2);
  1774. }
  1775. static void wcd939x_mbhc_moisture_detect_en(struct wcd_mbhc *mbhc, bool enable)
  1776. {
  1777. struct snd_soc_component *component = mbhc->component;
  1778. if (enable)
  1779. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1780. 0x0C, mbhc->moist_rref << 2);
  1781. else
  1782. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1783. 0x0C, R_OFF << 2);
  1784. }
  1785. static bool wcd939x_mbhc_get_moisture_status(struct wcd_mbhc *mbhc)
  1786. {
  1787. struct snd_soc_component *component = mbhc->component;
  1788. bool ret = false;
  1789. if ((mbhc->moist_rref == R_OFF) ||
  1790. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  1791. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1792. 0x0C, R_OFF << 2);
  1793. goto done;
  1794. }
  1795. /* Do not enable moisture detection if jack type is NC */
  1796. if (!mbhc->hphl_swh) {
  1797. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  1798. __func__);
  1799. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1800. 0x0C, R_OFF << 2);
  1801. goto done;
  1802. }
  1803. /*
  1804. * If moisture_en is already enabled, then skip to plug type
  1805. * detection.
  1806. */
  1807. if ((snd_soc_component_read(component, WCD939X_CTL_2) & 0x0C))
  1808. goto done;
  1809. wcd939x_mbhc_moisture_detect_en(mbhc, true);
  1810. /* Read moisture comparator status */
  1811. ret = ((snd_soc_component_read(component, WCD939X_FSM_STATUS)
  1812. & 0x20) ? 0 : 1);
  1813. done:
  1814. return ret;
  1815. }
  1816. static void wcd939x_mbhc_moisture_polling_ctrl(struct wcd_mbhc *mbhc,
  1817. bool enable)
  1818. {
  1819. struct snd_soc_component *component = mbhc->component;
  1820. snd_soc_component_update_bits(component,
  1821. WCD939X_MOISTURE_DET_POLLING_CTRL,
  1822. 0x04, (enable << 2));
  1823. }
  1824. static void wcd939x_mbhc_bcs_enable(struct wcd_mbhc *mbhc,
  1825. bool bcs_enable)
  1826. {
  1827. if (bcs_enable)
  1828. wcd939x_disable_bcs_before_slow_insert(mbhc->component, false);
  1829. else
  1830. wcd939x_disable_bcs_before_slow_insert(mbhc->component, true);
  1831. }
  1832. static void wcd939x_surge_reset_routine(struct wcd_mbhc *mbhc)
  1833. {
  1834. struct wcd939x_priv *wcd939x = snd_soc_component_get_drvdata(mbhc->component);
  1835. regcache_mark_dirty(wcd939x->regmap);
  1836. regcache_sync(wcd939x->regmap);
  1837. }
  1838. static void wcd939x_mbhc_zdet_leakage_resistance(struct wcd_mbhc *mbhc,
  1839. bool enable)
  1840. {
  1841. if (enable)
  1842. snd_soc_component_update_bits(mbhc->component, WCD939X_ZDET_BIAS_CTL,
  1843. 0x80, 0x80); /* disable 1M pull-up */
  1844. else
  1845. snd_soc_component_update_bits(mbhc->component, WCD939X_ZDET_BIAS_CTL,
  1846. 0x80, 0x00); /* enable 1M pull-up */
  1847. }
  1848. static const struct wcd_mbhc_cb mbhc_cb = {
  1849. .request_irq = wcd939x_mbhc_request_irq,
  1850. .irq_control = wcd939x_mbhc_irq_control,
  1851. .free_irq = wcd939x_mbhc_free_irq,
  1852. .clk_setup = wcd939x_mbhc_clk_setup,
  1853. .map_btn_code_to_num = wcd939x_mbhc_btn_to_num,
  1854. .mbhc_bias = wcd939x_mbhc_mbhc_bias_control,
  1855. .set_btn_thr = wcd939x_mbhc_program_btn_thr,
  1856. .lock_sleep = wcd939x_mbhc_lock_sleep,
  1857. .register_notifier = wcd939x_mbhc_register_notifier,
  1858. .micbias_enable_status = wcd939x_mbhc_micb_en_status,
  1859. .hph_pa_on_status = wcd939x_mbhc_hph_pa_on_status,
  1860. .hph_pull_up_control_v2 = wcd939x_mbhc_hph_l_pull_up_control,
  1861. .mbhc_micbias_control = wcd939x_mbhc_request_micbias,
  1862. .mbhc_micb_ramp_control = wcd939x_mbhc_micb_ramp_control,
  1863. .get_hwdep_fw_cal = wcd939x_get_hwdep_fw_cal,
  1864. .mbhc_micb_ctrl_thr_mic = wcd939x_mbhc_micb_ctrl_threshold_mic,
  1865. .compute_impedance = wcd939x_wcd_mbhc_calc_impedance,
  1866. .mbhc_gnd_det_ctrl = wcd939x_mbhc_gnd_det_ctrl,
  1867. .hph_pull_down_ctrl = wcd939x_mbhc_hph_pull_down_ctrl,
  1868. .mbhc_moisture_config = wcd939x_mbhc_moisture_config,
  1869. .mbhc_get_moisture_status = wcd939x_mbhc_get_moisture_status,
  1870. .mbhc_moisture_polling_ctrl = wcd939x_mbhc_moisture_polling_ctrl,
  1871. .mbhc_moisture_detect_en = wcd939x_mbhc_moisture_detect_en,
  1872. .bcs_enable = wcd939x_mbhc_bcs_enable,
  1873. .surge_reset_routine = wcd939x_surge_reset_routine,
  1874. .zdet_leakage_resistance = wcd939x_mbhc_zdet_leakage_resistance,
  1875. };
  1876. static int wcd939x_get_hph_type(struct snd_kcontrol *kcontrol,
  1877. struct snd_ctl_elem_value *ucontrol)
  1878. {
  1879. struct snd_soc_component *component =
  1880. snd_soc_kcontrol_component(kcontrol);
  1881. struct wcd939x_mbhc *wcd939x_mbhc = wcd939x_soc_get_mbhc(component);
  1882. struct wcd_mbhc *mbhc;
  1883. if (!wcd939x_mbhc) {
  1884. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1885. return -EINVAL;
  1886. }
  1887. mbhc = &wcd939x_mbhc->wcd_mbhc;
  1888. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  1889. dev_dbg(component->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  1890. return 0;
  1891. }
  1892. static int wcd939x_hph_impedance_get(struct snd_kcontrol *kcontrol,
  1893. struct snd_ctl_elem_value *ucontrol)
  1894. {
  1895. uint32_t zl, zr;
  1896. bool hphr;
  1897. struct soc_multi_mixer_control *mc;
  1898. struct snd_soc_component *component =
  1899. snd_soc_kcontrol_component(kcontrol);
  1900. struct wcd939x_mbhc *wcd939x_mbhc = wcd939x_soc_get_mbhc(component);
  1901. if (!wcd939x_mbhc) {
  1902. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1903. return -EINVAL;
  1904. }
  1905. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1906. hphr = mc->shift;
  1907. wcd_mbhc_get_impedance(&wcd939x_mbhc->wcd_mbhc, &zl, &zr);
  1908. dev_dbg(component->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  1909. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  1910. return 0;
  1911. }
  1912. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  1913. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  1914. wcd939x_get_hph_type, NULL),
  1915. };
  1916. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  1917. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  1918. wcd939x_hph_impedance_get, NULL),
  1919. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  1920. wcd939x_hph_impedance_get, NULL),
  1921. };
  1922. /*
  1923. * wcd939x_mbhc_get_impedance: get impedance of headphone
  1924. * left and right channels
  1925. * @wcd939x_mbhc: handle to struct wcd939x_mbhc *
  1926. * @zl: handle to left-ch impedance
  1927. * @zr: handle to right-ch impedance
  1928. * return 0 for success or error code in case of failure
  1929. */
  1930. int wcd939x_mbhc_get_impedance(struct wcd939x_mbhc *wcd939x_mbhc,
  1931. uint32_t *zl, uint32_t *zr)
  1932. {
  1933. if (!wcd939x_mbhc) {
  1934. pr_err_ratelimited("%s: mbhc not initialized!\n", __func__);
  1935. return -EINVAL;
  1936. }
  1937. if (!zl || !zr) {
  1938. pr_err_ratelimited("%s: zl or zr null!\n", __func__);
  1939. return -EINVAL;
  1940. }
  1941. return wcd_mbhc_get_impedance(&wcd939x_mbhc->wcd_mbhc, zl, zr);
  1942. }
  1943. EXPORT_SYMBOL(wcd939x_mbhc_get_impedance);
  1944. /*
  1945. * wcd939x_mbhc_hs_detect: starts mbhc insertion/removal functionality
  1946. * @codec: handle to snd_soc_component *
  1947. * @mbhc_cfg: handle to mbhc configuration structure
  1948. * return 0 if mbhc_start is success or error code in case of failure
  1949. */
  1950. int wcd939x_mbhc_hs_detect(struct snd_soc_component *component,
  1951. struct wcd_mbhc_config *mbhc_cfg)
  1952. {
  1953. struct wcd939x_priv *wcd939x = NULL;
  1954. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  1955. if (!component) {
  1956. pr_err_ratelimited("%s: component is NULL\n", __func__);
  1957. return -EINVAL;
  1958. }
  1959. wcd939x = snd_soc_component_get_drvdata(component);
  1960. if (!wcd939x) {
  1961. pr_err_ratelimited("%s: wcd939x is NULL\n", __func__);
  1962. return -EINVAL;
  1963. }
  1964. wcd939x_mbhc = wcd939x->mbhc;
  1965. if (!wcd939x_mbhc) {
  1966. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1967. return -EINVAL;
  1968. }
  1969. return wcd_mbhc_start(&wcd939x_mbhc->wcd_mbhc, mbhc_cfg);
  1970. }
  1971. EXPORT_SYMBOL(wcd939x_mbhc_hs_detect);
  1972. /*
  1973. * wcd939x_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  1974. * @component: handle to snd_soc_component *
  1975. */
  1976. void wcd939x_mbhc_hs_detect_exit(struct snd_soc_component *component)
  1977. {
  1978. struct wcd939x_priv *wcd939x = NULL;
  1979. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  1980. if (!component) {
  1981. pr_err_ratelimited("%s: component is NULL\n", __func__);
  1982. return;
  1983. }
  1984. wcd939x = snd_soc_component_get_drvdata(component);
  1985. if (!wcd939x) {
  1986. pr_err_ratelimited("%s: wcd939x is NULL\n", __func__);
  1987. return;
  1988. }
  1989. wcd939x_mbhc = wcd939x->mbhc;
  1990. if (!wcd939x_mbhc) {
  1991. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1992. return;
  1993. }
  1994. wcd_mbhc_stop(&wcd939x_mbhc->wcd_mbhc);
  1995. }
  1996. EXPORT_SYMBOL(wcd939x_mbhc_hs_detect_exit);
  1997. /*
  1998. * wcd939x_mbhc_ssr_down: stop mbhc during
  1999. * wcd939x subsystem restart
  2000. * mbhc: pointer to wcd937x_mbhc structure
  2001. * component: handle to snd_soc_component *
  2002. */
  2003. void wcd939x_mbhc_ssr_down(struct wcd939x_mbhc *mbhc,
  2004. struct snd_soc_component *component)
  2005. {
  2006. struct wcd_mbhc *wcd_mbhc = NULL;
  2007. if (!mbhc || !component)
  2008. return;
  2009. wcd_mbhc = &mbhc->wcd_mbhc;
  2010. if (!wcd_mbhc) {
  2011. dev_err_ratelimited(component->dev, "%s: wcd_mbhc is NULL\n", __func__);
  2012. return;
  2013. }
  2014. wcd939x_mbhc_hs_detect_exit(component);
  2015. wcd_mbhc_deinit(wcd_mbhc);
  2016. }
  2017. EXPORT_SYMBOL(wcd939x_mbhc_ssr_down);
  2018. /*
  2019. * wcd939x_mbhc_post_ssr_init: initialize mbhc for
  2020. * wcd939x post subsystem restart
  2021. * @mbhc: poniter to wcd939x_mbhc structure
  2022. * @component: handle to snd_soc_component *
  2023. *
  2024. * return 0 if mbhc_init is success or error code in case of failure
  2025. */
  2026. int wcd939x_mbhc_post_ssr_init(struct wcd939x_mbhc *mbhc,
  2027. struct snd_soc_component *component)
  2028. {
  2029. int ret = 0;
  2030. struct wcd_mbhc *wcd_mbhc = NULL;
  2031. if (!mbhc || !component)
  2032. return -EINVAL;
  2033. wcd_mbhc = &mbhc->wcd_mbhc;
  2034. if (wcd_mbhc == NULL) {
  2035. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  2036. return -EINVAL;
  2037. }
  2038. /* Reset detection type to insertion after SSR recovery */
  2039. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  2040. 0x20, 0x20);
  2041. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb, &intr_ids,
  2042. wcd_mbhc_registers, WCD939X_ZDET_SUPPORTED);
  2043. if (ret) {
  2044. dev_err(component->dev, "%s: mbhc initialization failed\n",
  2045. __func__);
  2046. goto done;
  2047. }
  2048. done:
  2049. return ret;
  2050. }
  2051. EXPORT_SYMBOL(wcd939x_mbhc_post_ssr_init);
  2052. /*
  2053. * wcd939x_mbhc_init: initialize mbhc for wcd939x
  2054. * @mbhc: poniter to wcd939x_mbhc struct pointer to store the configs
  2055. * @codec: handle to snd_soc_component *
  2056. * @fw_data: handle to firmware data
  2057. *
  2058. * return 0 if mbhc_init is success or error code in case of failure
  2059. */
  2060. int wcd939x_mbhc_init(struct wcd939x_mbhc **mbhc,
  2061. struct snd_soc_component *component,
  2062. struct fw_info *fw_data)
  2063. {
  2064. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  2065. struct wcd_mbhc *wcd_mbhc = NULL;
  2066. int ret = 0;
  2067. struct wcd939x_pdata *pdata;
  2068. struct wcd939x_priv *wcd939x;
  2069. if (!component) {
  2070. pr_err("%s: component is NULL\n", __func__);
  2071. return -EINVAL;
  2072. }
  2073. wcd939x_mbhc = devm_kzalloc(component->dev, sizeof(struct wcd939x_mbhc),
  2074. GFP_KERNEL);
  2075. if (!wcd939x_mbhc)
  2076. return -ENOMEM;
  2077. wcd939x_mbhc->fw_data = fw_data;
  2078. BLOCKING_INIT_NOTIFIER_HEAD(&wcd939x_mbhc->notifier);
  2079. wcd_mbhc = &wcd939x_mbhc->wcd_mbhc;
  2080. if (wcd_mbhc == NULL) {
  2081. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  2082. ret = -EINVAL;
  2083. goto err;
  2084. }
  2085. /* Setting default mbhc detection logic to ADC */
  2086. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  2087. /* Down ramp timer set-up */
  2088. timer_setup(&wcd939x_mbhc->rdown_timer, rdown_timer_callback, 0);
  2089. wcd939x_mbhc->rdown_prev_iter = 0;
  2090. wcd939x_mbhc->rdown_timer_complete = false;
  2091. pdata = dev_get_platdata(component->dev);
  2092. if (!pdata) {
  2093. dev_err(component->dev, "%s: pdata pointer is NULL\n",
  2094. __func__);
  2095. ret = -EINVAL;
  2096. goto err;
  2097. }
  2098. wcd_mbhc->micb_mv = pdata->micbias.micb2_mv;
  2099. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb,
  2100. &intr_ids, wcd_mbhc_registers,
  2101. WCD939X_ZDET_SUPPORTED);
  2102. if (ret) {
  2103. dev_err(component->dev, "%s: mbhc initialization failed\n",
  2104. __func__);
  2105. goto err;
  2106. }
  2107. (*mbhc) = wcd939x_mbhc;
  2108. snd_soc_add_component_controls(component, impedance_detect_controls,
  2109. ARRAY_SIZE(impedance_detect_controls));
  2110. snd_soc_add_component_controls(component, hph_type_detect_controls,
  2111. ARRAY_SIZE(hph_type_detect_controls));
  2112. wcd939x = dev_get_drvdata(component->dev);
  2113. if (!wcd939x) {
  2114. dev_err(component->dev, "%s: wcd939x pointer is NULL\n", __func__);
  2115. ret = -EINVAL;
  2116. goto err;
  2117. }
  2118. usbcss_hs_sysfs_init(wcd939x);
  2119. return 0;
  2120. err:
  2121. if (wcd939x_mbhc)
  2122. del_timer(&wcd939x_mbhc->rdown_timer);
  2123. devm_kfree(component->dev, wcd939x_mbhc);
  2124. return ret;
  2125. }
  2126. EXPORT_SYMBOL(wcd939x_mbhc_init);
  2127. /*
  2128. * wcd939x_mbhc_deinit: deinitialize mbhc for wcd939x
  2129. * @codec: handle to snd_soc_component *
  2130. */
  2131. void wcd939x_mbhc_deinit(struct snd_soc_component *component)
  2132. {
  2133. struct wcd939x_priv *wcd939x;
  2134. struct wcd939x_mbhc *wcd939x_mbhc;
  2135. if (!component) {
  2136. pr_err("%s: component is NULL\n", __func__);
  2137. return;
  2138. }
  2139. wcd939x = snd_soc_component_get_drvdata(component);
  2140. if (!wcd939x) {
  2141. pr_err("%s: wcd939x is NULL\n", __func__);
  2142. return;
  2143. }
  2144. wcd939x_mbhc = wcd939x->mbhc;
  2145. if (wcd939x_mbhc) {
  2146. del_timer(&wcd939x_mbhc->rdown_timer);
  2147. wcd_mbhc_deinit(&wcd939x_mbhc->wcd_mbhc);
  2148. devm_kfree(component->dev, wcd939x_mbhc);
  2149. }
  2150. }
  2151. EXPORT_SYMBOL(wcd939x_mbhc_deinit);