wcd939x-mbhc.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/device.h>
  10. #include <linux/printk.h>
  11. #include <linux/ratelimit.h>
  12. #include <linux/kernel.h>
  13. #include <linux/gpio.h>
  14. #include <linux/delay.h>
  15. #include <linux/regmap.h>
  16. #include <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 is the table upper bound, no interpolation needed, just use the lb corr factor */
  746. if ((lb + 1) >= ARRAY_SIZE(zdet_dnl_table)) {
  747. z_interp = (s64) ((flag_se_diff) ? (zdet_dnl_table[lb].diff_corr_mohms) :
  748. (zdet_dnl_table[lb].se_corr_mohms));
  749. goto apply_interpolated_bias;
  750. }
  751. /* Set up interpolation */
  752. lb_to_ub = OHMS_TO_MILLIOHMS *
  753. (s64) (u64) ((zdet_dnl_table[lb + 1].base_val_ohms -
  754. zdet_dnl_table[lb].base_val_ohms));
  755. z_to_ub = (OHMS_TO_MILLIOHMS *
  756. ((s64) (u64) (zdet_dnl_table[lb + 1].base_val_ohms))) - z_meas_bias_removed;
  757. lb_to_z = z_meas_bias_removed - (OHMS_TO_MILLIOHMS *
  758. ((s64) (u64) (zdet_dnl_table[lb].base_val_ohms)));
  759. lb_corr = (s64) ((flag_se_diff) ? (zdet_dnl_table[lb].diff_corr_mohms) :
  760. (zdet_dnl_table[lb].se_corr_mohms));
  761. ub_corr = (s64) ((flag_se_diff) ? (zdet_dnl_table[lb + 1].diff_corr_mohms) :
  762. (zdet_dnl_table[lb + 1].se_corr_mohms));
  763. /* Linear interpolation */
  764. z_interp = (lb_corr * z_to_ub + ub_corr * lb_to_z) / lb_to_ub;
  765. apply_interpolated_bias:
  766. /* Subtract interpolated bias to correct error */
  767. if (z_interp < z_val_slope_corrected)
  768. *z = (u32) (s32) (z_val_slope_corrected - z_interp);
  769. }
  770. static int get_lb_zdet_base_val_index(uint32_t z_val)
  771. {
  772. int i;
  773. /* Find the lower bound index, whose base value is the smallest value that is still higher
  774. * than the load
  775. */
  776. for (i = 1; i < ARRAY_SIZE(zdet_dnl_table); i++) {
  777. if (z_val < (OHMS_TO_MILLIOHMS * (u32) zdet_dnl_table[i].base_val_ohms))
  778. return i - 1;
  779. }
  780. /* Return the last index if the load is larger than all base values */
  781. return ARRAY_SIZE(zdet_dnl_table) - 1;
  782. }
  783. static void apply_zdet_correction(uint32_t *z, int flag_se_diff, u32 se_slope_factor_times_1000,
  784. u32 diff_slope_factor_times_1000)
  785. {
  786. s64 z_val_slope_corrected = 0, slope_corr = 0;
  787. uint32_t z_meas_bias_removed = 0;
  788. int lb;
  789. /* Apply slope correction */
  790. slope_corr = (s64) ((flag_se_diff) ? diff_slope_factor_times_1000 :
  791. se_slope_factor_times_1000);
  792. z_val_slope_corrected = ((s64) (u64) *z) * (FLOAT_TO_FIXED_XTALK) * slope_corr /
  793. SLOPE_FACTOR_SCALER / (FLOAT_TO_FIXED_XTALK);
  794. /* Interpolate correction term to bias out and apply correction */
  795. z_meas_bias_removed = (flag_se_diff) ? *z - DIFF_SLOPE_MEAS_BIAS : *z - SE_SLOPE_MEAS_BIAS;
  796. lb = get_lb_zdet_base_val_index(z_meas_bias_removed);
  797. interpolate_zdet_val(z, (s64) (u64) z_meas_bias_removed, z_val_slope_corrected, lb,
  798. flag_se_diff);
  799. }
  800. static void get_r_common_gnd(struct wcd939x_priv *wcd939x, u32 r_gnd_res_tot_mohms,
  801. u32 r_gnd_int_fet_mohms, u32 r_gnd_par_route1_mohms)
  802. {
  803. u32 r_common_gnd_mohms = 0, r_accum = 0, r_avg = 0;
  804. size_t i;
  805. struct wcd939x_pdata *pdata = dev_get_platdata(wcd939x->dev);
  806. size_t index = pdata->usbcss_hs.gnd.r_cm_gnd_buffer.write_index;
  807. r_common_gnd_mohms = r_gnd_res_tot_mohms - r_gnd_int_fet_mohms - r_gnd_par_route1_mohms;
  808. /* Compute average from r_common_gnd buffer */
  809. for (i = 0; i < R_COMMON_GND_BUFFER_SIZE; i++)
  810. r_accum += pdata->usbcss_hs.gnd.r_cm_gnd_buffer.data[i];
  811. r_avg = r_accum / R_COMMON_GND_BUFFER_SIZE;
  812. /* If r_common_gnd_mohms is OOB, use the average of the buffer values instead */
  813. if (r_common_gnd_mohms > (r_avg + pdata->usbcss_hs.gnd.r_common_gnd_margin) ||
  814. (r_avg >= pdata->usbcss_hs.gnd.r_common_gnd_margin &&
  815. r_common_gnd_mohms < (r_avg - pdata->usbcss_hs.gnd.r_common_gnd_margin))) {
  816. dev_dbg(wcd939x->dev, "%s: %s %d %s %d %s\n", __func__,
  817. "The average of the r_common_gnd buffer,", r_avg,
  818. "mohms, is being used instead of the calculated r_common_gnd value of",
  819. r_common_gnd_mohms, "mohms");
  820. pdata->usbcss_hs.gnd.r_common_gnd_mohms = r_avg;
  821. return;
  822. }
  823. /* Otherwise, use the computed value and store it in the buffer, updating the write index */
  824. pdata->usbcss_hs.gnd.r_common_gnd_mohms = r_common_gnd_mohms;
  825. pdata->usbcss_hs.gnd.r_cm_gnd_buffer.data[index] = r_common_gnd_mohms;
  826. pdata->usbcss_hs.gnd.r_cm_gnd_buffer.write_index = (index + 1) % R_COMMON_GND_BUFFER_SIZE;
  827. dev_dbg(wcd939x->dev, "%s: %s %d %s\n", __func__, "The calculated r_common_gnd value,",
  828. r_common_gnd_mohms, "mohms, is being used");
  829. }
  830. struct usbcss_hs_attr {
  831. struct wcd939x_priv *priv;
  832. struct kobj_attribute attr;
  833. int index;
  834. };
  835. static char *usbcss_sysfs_files[] = {
  836. "rdson_3p6v",
  837. "rdson_6v",
  838. "r1_l",
  839. "r1_r",
  840. "r3_l",
  841. "r3_r",
  842. "r4_sbu1",
  843. "r4_sbu2",
  844. "r5_sbu1",
  845. "r5_sbu2",
  846. "r6_sbu1",
  847. "r6_sbu2",
  848. "r7_sbu1",
  849. "r7_sbu2",
  850. "r_common_gnd_offset",
  851. "rcom_margin",
  852. "se_slope_factor_times_1000",
  853. "diff_slope_factor_times_1000",
  854. "lin_k_aud",
  855. "xtalk_config",
  856. };
  857. static ssize_t usbcss_sysfs_store(struct kobject *kobj, struct kobj_attribute *attr,
  858. const char *buf, size_t count)
  859. {
  860. struct usbcss_hs_attr *usbc_attr;
  861. struct wcd939x_priv *wcd939x;
  862. struct wcd939x_pdata *pdata;
  863. struct wcd939x_usbcss_hs_params *usbcss_hs;
  864. long val;
  865. int rc;
  866. u32 aud_tap = 0;
  867. bool update_xtalk = false, update_linearizer = false;
  868. usbc_attr = container_of(attr, struct usbcss_hs_attr, attr);
  869. wcd939x = usbc_attr->priv;
  870. pdata = dev_get_platdata(wcd939x->dev);
  871. if (!wcd939x || !pdata)
  872. return -EINVAL;
  873. usbcss_hs = &pdata->usbcss_hs;
  874. rc = kstrtol(buf, 0, &val);
  875. if (rc)
  876. return rc;
  877. if (strcmp(attr->attr.name, "rdson_3p6v") == 0) {
  878. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  879. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  880. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  881. return count;
  882. }
  883. usbcss_hs->gnd.rdson_3p6v_mohms = val;
  884. usbcss_hs->gnd.gnd_ext_fet_delta_mohms = (s32) (usbcss_hs->gnd.rdson_3p6v_mohms -
  885. usbcss_hs->gnd.rdson_mohms);
  886. update_linearizer = usbcss_hs->xtalk.xtalk_config == XTALK_ANALOG;
  887. } else if (strcmp(attr->attr.name, "rdson_6v") == 0) {
  888. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  889. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  890. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  891. return count;
  892. }
  893. usbcss_hs->gnd.rdson_mohms = val;
  894. update_linearizer = usbcss_hs->xtalk.xtalk_config == XTALK_ANALOG;
  895. } else if (strcmp(attr->attr.name, "r1_l") == 0) {
  896. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  897. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  898. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  899. return count;
  900. }
  901. usbcss_hs->aud.l.r1 = val;
  902. update_xtalk = true;
  903. } else if (strcmp(attr->attr.name, "r1_r") == 0) {
  904. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  905. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  906. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  907. return count;
  908. }
  909. usbcss_hs->aud.r.r1 = val;
  910. update_xtalk = true;
  911. } else if (strcmp(attr->attr.name, "r3_l") == 0) {
  912. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  913. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  914. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  915. return count;
  916. }
  917. usbcss_hs->aud.l.r3 = val;
  918. update_linearizer = true;
  919. } else if (strcmp(attr->attr.name, "r3_r") == 0) {
  920. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  921. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  922. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  923. return count;
  924. }
  925. usbcss_hs->aud.r.r3 = val;
  926. update_linearizer = true;
  927. } else if (strcmp(attr->attr.name, "r4_sbu1") == 0) {
  928. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  929. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  930. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  931. return count;
  932. }
  933. usbcss_hs->gnd.sbu1.r4 = val;
  934. update_xtalk = true;
  935. update_linearizer = true;
  936. } else if (strcmp(attr->attr.name, "r4_sbu2") == 0) {
  937. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  938. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  939. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  940. return count;
  941. }
  942. usbcss_hs->gnd.sbu2.r4 = val;
  943. update_xtalk = true;
  944. update_linearizer = true;
  945. } else if (strcmp(attr->attr.name, "r5_sbu1") == 0) {
  946. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  947. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  948. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  949. return count;
  950. }
  951. usbcss_hs->gnd.sbu1.r5 = val;
  952. switch (usbcss_hs->xtalk.xtalk_config) {
  953. case XTALK_ANALOG:
  954. update_xtalk = true;
  955. update_linearizer = true;
  956. break;
  957. case XTALK_DIGITAL:
  958. fallthrough;
  959. case XTALK_NONE:
  960. fallthrough;
  961. default:
  962. return count;
  963. }
  964. } else if (strcmp(attr->attr.name, "r5_sbu2") == 0) {
  965. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  966. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  967. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  968. return count;
  969. }
  970. usbcss_hs->gnd.sbu2.r5 = val;
  971. switch (usbcss_hs->xtalk.xtalk_config) {
  972. case XTALK_ANALOG:
  973. update_xtalk = true;
  974. update_linearizer = true;
  975. break;
  976. case XTALK_DIGITAL:
  977. fallthrough;
  978. case XTALK_NONE:
  979. fallthrough;
  980. default:
  981. return count;
  982. }
  983. } else if (strcmp(attr->attr.name, "r6_sbu1") == 0) {
  984. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  985. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  986. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  987. return count;
  988. }
  989. usbcss_hs->gnd.sbu1.r6 = val;
  990. switch (usbcss_hs->xtalk.xtalk_config) {
  991. case XTALK_DIGITAL:
  992. update_xtalk = true;
  993. update_linearizer = true;
  994. break;
  995. case XTALK_ANALOG:
  996. fallthrough;
  997. case XTALK_NONE:
  998. fallthrough;
  999. default:
  1000. return count;
  1001. }
  1002. } else if (strcmp(attr->attr.name, "r6_sbu2") == 0) {
  1003. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1004. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1005. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1006. return count;
  1007. }
  1008. usbcss_hs->gnd.sbu2.r6 = val;
  1009. switch (usbcss_hs->xtalk.xtalk_config) {
  1010. case XTALK_DIGITAL:
  1011. update_xtalk = true;
  1012. update_linearizer = true;
  1013. break;
  1014. case XTALK_ANALOG:
  1015. fallthrough;
  1016. case XTALK_NONE:
  1017. fallthrough;
  1018. default:
  1019. return count;
  1020. }
  1021. } else if (strcmp(attr->attr.name, "r7_sbu1") == 0) {
  1022. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1023. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1024. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1025. return count;
  1026. }
  1027. usbcss_hs->gnd.sbu1.r7 = val;
  1028. switch (usbcss_hs->xtalk.xtalk_config) {
  1029. case XTALK_DIGITAL:
  1030. update_xtalk = true;
  1031. update_linearizer = true;
  1032. break;
  1033. case XTALK_ANALOG:
  1034. fallthrough;
  1035. case XTALK_NONE:
  1036. fallthrough;
  1037. default:
  1038. return count;
  1039. }
  1040. } else if (strcmp(attr->attr.name, "r7_sbu2") == 0) {
  1041. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1042. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1043. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1044. return count;
  1045. }
  1046. usbcss_hs->gnd.sbu2.r7 = val;
  1047. switch (usbcss_hs->xtalk.xtalk_config) {
  1048. case XTALK_DIGITAL:
  1049. update_xtalk = true;
  1050. update_linearizer = true;
  1051. break;
  1052. case XTALK_ANALOG:
  1053. fallthrough;
  1054. case XTALK_NONE:
  1055. fallthrough;
  1056. default:
  1057. return count;
  1058. }
  1059. } else if (strcmp(attr->attr.name, "r_common_gnd_offset") == 0) {
  1060. if (val < -MAX_USBCSS_HS_IMPEDANCE_MOHMS || val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1061. dev_err(wcd939x->dev, "%s: Value %d out of bounds. Min: %d, Max: %d\n",
  1062. __func__, val, -MAX_USBCSS_HS_IMPEDANCE_MOHMS,
  1063. MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1064. return count;
  1065. }
  1066. usbcss_hs->gnd.r_common_gnd_offset = val;
  1067. update_xtalk = true;
  1068. } else if (strcmp(attr->attr.name, "rcom_margin") == 0) {
  1069. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1070. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1071. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1072. return count;
  1073. }
  1074. usbcss_hs->gnd.r_common_gnd_margin = val;
  1075. } else if (strcmp(attr->attr.name, "se_slope_factor_times_1000") == 0) {
  1076. if (val > MAX_USBCSS_HS_IMPEDANCE_MOHMS) {
  1077. dev_err(wcd939x->dev, "%s: Value %d out of HS impedance range %d\n",
  1078. __func__, val, MAX_USBCSS_HS_IMPEDANCE_MOHMS);
  1079. return count;
  1080. }
  1081. usbcss_hs->se_slope_factor_times_1000 = val;
  1082. } else if (strcmp(attr->attr.name, "diff_slope_factor_times_1000") == 0) {
  1083. if (val > MAX_DIFF_SLOPE_FACTOR || val < MIN_DIFF_SLOPE_FACTOR) {
  1084. dev_err(wcd939x->dev, "%s: Value %d out of range of %d to %d\n",
  1085. __func__, val, MIN_DIFF_SLOPE_FACTOR, MAX_DIFF_SLOPE_FACTOR);
  1086. return count;
  1087. }
  1088. usbcss_hs->diff_slope_factor_times_1000 = val;
  1089. } else if (strcmp(attr->attr.name, "lin_k_aud") == 0) {
  1090. if (val < MIN_K_TIMES_100 || val > MAX_K_TIMES_100) {
  1091. dev_err(wcd939x->dev, "%s: Value %d out of bounds. Min: %d, Max: %d\n",
  1092. __func__, val, MIN_K_TIMES_100, MAX_K_TIMES_100);
  1093. return count;
  1094. }
  1095. usbcss_hs->aud.k_aud_times_100 = val;
  1096. update_linearizer = true;
  1097. } else if (strcmp(attr->attr.name, "xtalk_config") == 0) {
  1098. pdata->usbcss_hs.xtalk.xtalk_config = val;
  1099. update_xtalk = true;
  1100. switch (val) {
  1101. case XTALK_NONE:
  1102. usbcss_hs->xtalk.scale_l = MAX_XTALK_SCALE;
  1103. usbcss_hs->xtalk.scale_r = MAX_XTALK_SCALE;
  1104. usbcss_hs->xtalk.alpha_l = MIN_XTALK_ALPHA;
  1105. usbcss_hs->xtalk.alpha_r = MIN_XTALK_ALPHA;
  1106. break;
  1107. case XTALK_DIGITAL:
  1108. update_linearizer = true;
  1109. break;
  1110. case XTALK_ANALOG:
  1111. update_linearizer = true;
  1112. break;
  1113. default:
  1114. return count;
  1115. }
  1116. }
  1117. /* Update parastics */
  1118. switch (pdata->usbcss_hs.xtalk.xtalk_config) {
  1119. case XTALK_NONE:
  1120. fallthrough;
  1121. case XTALK_DIGITAL:
  1122. usbcss_hs->gnd.sbu1.r_gnd_par_route1_mohms = usbcss_hs->gnd.sbu1.r7;
  1123. usbcss_hs->gnd.sbu2.r_gnd_par_route1_mohms = usbcss_hs->gnd.sbu2.r7;
  1124. usbcss_hs->gnd.sbu1.r_gnd_par_route2_mohms = usbcss_hs->gnd.sbu1.r6 +
  1125. usbcss_hs->gnd.sbu1.r4;
  1126. usbcss_hs->gnd.sbu2.r_gnd_par_route2_mohms = usbcss_hs->gnd.sbu2.r6 +
  1127. usbcss_hs->gnd.sbu2.r4;
  1128. break;
  1129. case XTALK_ANALOG:
  1130. usbcss_hs->gnd.sbu1.r_gnd_par_route1_mohms = usbcss_hs->gnd.sbu1.r5 +
  1131. usbcss_hs->gnd.sbu1.r4;
  1132. usbcss_hs->gnd.sbu2.r_gnd_par_route1_mohms = usbcss_hs->gnd.sbu2.r5 +
  1133. usbcss_hs->gnd.sbu2.r4;
  1134. usbcss_hs->gnd.sbu1.r_gnd_par_route2_mohms = 1;
  1135. usbcss_hs->gnd.sbu2.r_gnd_par_route2_mohms = 1;
  1136. break;
  1137. default:
  1138. return count;
  1139. }
  1140. /* Update parastics total */
  1141. usbcss_hs->gnd.sbu1.r_gnd_par_tot_mohms = usbcss_hs->gnd.sbu1.r_gnd_par_route1_mohms +
  1142. usbcss_hs->gnd.sbu1.r_gnd_par_route2_mohms;
  1143. usbcss_hs->gnd.sbu2.r_gnd_par_tot_mohms = usbcss_hs->gnd.sbu2.r_gnd_par_route1_mohms +
  1144. usbcss_hs->gnd.sbu2.r_gnd_par_route2_mohms;
  1145. if (update_xtalk) {
  1146. /* Apply r_common_gnd offset */
  1147. usbcss_hs->gnd.r_common_gnd_mohms = (usbcss_hs->gnd.r_common_gnd_offset >= 0) ?
  1148. usbcss_hs->gnd.r_common_gnd_mohms +
  1149. (u32) usbcss_hs->gnd.r_common_gnd_offset :
  1150. usbcss_hs->gnd.r_common_gnd_mohms -
  1151. (u32) (-1 * usbcss_hs->gnd.r_common_gnd_offset);
  1152. /* Compute and store xtalk values */
  1153. update_xtalk_scale_and_alpha(wcd939x);
  1154. /* Revert r_common_gnd offset */
  1155. usbcss_hs->gnd.r_common_gnd_mohms = (usbcss_hs->gnd.r_common_gnd_offset >= 0) ?
  1156. usbcss_hs->gnd.r_common_gnd_mohms -
  1157. (u32) usbcss_hs->gnd.r_common_gnd_offset :
  1158. usbcss_hs->gnd.r_common_gnd_mohms +
  1159. (u32) (-1 * usbcss_hs->gnd.r_common_gnd_offset);
  1160. /* Apply xtalk scale and alpha values */
  1161. regmap_update_bits(wcd939x->regmap, WCD939X_HPHL_RX_PATH_SEC0, 0x1F,
  1162. pdata->usbcss_hs.xtalk.scale_l);
  1163. regmap_update_bits(wcd939x->regmap, WCD939X_HPHL_RX_PATH_SEC1, 0xFF,
  1164. pdata->usbcss_hs.xtalk.alpha_l);
  1165. regmap_update_bits(wcd939x->regmap, WCD939X_HPHL_RX_PATH_SEC0 +
  1166. XTALK_CH_REG_ADDR_DELTA, 0x1F,
  1167. pdata->usbcss_hs.xtalk.scale_r);
  1168. regmap_update_bits(wcd939x->regmap, WCD939X_HPHL_RX_PATH_SEC1 +
  1169. XTALK_CH_REG_ADDR_DELTA, 0xFF,
  1170. pdata->usbcss_hs.xtalk.alpha_r);
  1171. dev_err(wcd939x->dev, "%s: Updated xtalk thru sysfs\n",
  1172. __func__);
  1173. dev_dbg(wcd939x->dev, "%s: Left-channel: Xtalk scale is 0x%x and alpha is 0x%x\n",
  1174. __func__, pdata->usbcss_hs.xtalk.scale_l, pdata->usbcss_hs.xtalk.alpha_l);
  1175. dev_dbg(wcd939x->dev, "%s: Right-channel: Xtalk scale is 0x%x and alpha is 0x%x\n",
  1176. __func__, pdata->usbcss_hs.xtalk.scale_r, pdata->usbcss_hs.xtalk.alpha_r);
  1177. }
  1178. if (update_linearizer) {
  1179. get_linearizer_taps(pdata, &aud_tap);
  1180. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1181. wcd_usbss_set_linearizer_sw_tap(aud_tap, LINEARIZER_DEFAULT_TAP);
  1182. #endif
  1183. dev_err(wcd939x->dev, "%s: Updated linearizer thru sysfs\n",
  1184. __func__);
  1185. dev_dbg(wcd939x->dev, "%s: Linearizer aud_tap is 0x%x\n",
  1186. __func__, aud_tap);
  1187. }
  1188. return count;
  1189. }
  1190. static ssize_t usbcss_sysfs_show(struct kobject *kobj,
  1191. struct kobj_attribute *attr, char *buf)
  1192. {
  1193. struct usbcss_hs_attr *usbc_attr;
  1194. struct wcd939x_priv *wcd939x;
  1195. struct wcd939x_pdata *pdata;
  1196. usbc_attr = container_of(attr, struct usbcss_hs_attr, attr);
  1197. wcd939x = usbc_attr->priv;
  1198. pdata = dev_get_platdata(wcd939x->dev);
  1199. if (strcmp(attr->attr.name, "rdson_3p6v") == 0)
  1200. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.rdson_3p6v_mohms);
  1201. else if (strcmp(attr->attr.name, "rdson_6v") == 0)
  1202. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.rdson_mohms);
  1203. else if (strcmp(attr->attr.name, "r1_l") == 0)
  1204. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.l.r1);
  1205. else if (strcmp(attr->attr.name, "r1_r") == 0)
  1206. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.r.r1);
  1207. else if (strcmp(attr->attr.name, "r3_l") == 0)
  1208. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.l.r3);
  1209. else if (strcmp(attr->attr.name, "r3_r") == 0)
  1210. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.r.r3);
  1211. else if (strcmp(attr->attr.name, "r4_sbu1") == 0)
  1212. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu1.r4);
  1213. else if (strcmp(attr->attr.name, "r4_sbu2") == 0)
  1214. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu2.r4);
  1215. else if (strcmp(attr->attr.name, "r5_sbu1") == 0)
  1216. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu1.r5);
  1217. else if (strcmp(attr->attr.name, "r5_sbu2") == 0)
  1218. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu2.r5);
  1219. else if (strcmp(attr->attr.name, "r6_sbu1") == 0)
  1220. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu1.r6);
  1221. else if (strcmp(attr->attr.name, "r6_sbu2") == 0)
  1222. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu2.r6);
  1223. else if (strcmp(attr->attr.name, "r7_sbu1") == 0)
  1224. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu1.r7);
  1225. else if (strcmp(attr->attr.name, "r7_sbu2") == 0)
  1226. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.sbu2.r7);
  1227. else if (strcmp(attr->attr.name, "r_common_gnd_offset") == 0)
  1228. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.r_common_gnd_offset);
  1229. else if (strcmp(attr->attr.name, "rcom_margin") == 0)
  1230. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.gnd.r_common_gnd_margin);
  1231. else if (strcmp(attr->attr.name, "se_slope_factor_times_1000") == 0)
  1232. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.se_slope_factor_times_1000);
  1233. else if (strcmp(attr->attr.name, "diff_slope_factor_times_1000") == 0)
  1234. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.diff_slope_factor_times_1000);
  1235. else if (strcmp(attr->attr.name, "lin_k_aud") == 0)
  1236. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.aud.k_aud_times_100);
  1237. else if (strcmp(attr->attr.name, "xtalk_config") == 0)
  1238. return scnprintf(buf, 10, "%d\n", pdata->usbcss_hs.xtalk.xtalk_config);
  1239. return 0;
  1240. }
  1241. static int create_sysfs_entry_file(struct wcd939x_priv *wcd939x, char *name, int mode,
  1242. int index, struct kobject *parent)
  1243. {
  1244. struct usbcss_hs_attr *usbc_attr;
  1245. char *name_copy;
  1246. usbc_attr = devm_kmalloc(wcd939x->dev, sizeof(*usbc_attr), GFP_KERNEL);
  1247. if (!usbc_attr)
  1248. return -ENOMEM;
  1249. name_copy = devm_kstrdup(wcd939x->dev, name, GFP_KERNEL);
  1250. if (!name_copy)
  1251. return -ENOMEM;
  1252. usbc_attr->priv = wcd939x;
  1253. usbc_attr->index = index;
  1254. usbc_attr->attr.attr.name = name_copy;
  1255. usbc_attr->attr.attr.mode = mode;
  1256. usbc_attr->attr.show = usbcss_sysfs_show;
  1257. usbc_attr->attr.store = usbcss_sysfs_store;
  1258. sysfs_attr_init(&usbc_attr->attr.attr);
  1259. return sysfs_create_file(parent, &usbc_attr->attr.attr);
  1260. }
  1261. static int usbcss_hs_sysfs_init(struct wcd939x_priv *wcd939x)
  1262. {
  1263. int rc = 0;
  1264. int i = 0;
  1265. struct kobject *kobj = NULL;
  1266. if (!wcd939x || !wcd939x->dev) {
  1267. pr_err("%s: Invalid wcd939x private data.\n", __func__);
  1268. return -EINVAL;
  1269. }
  1270. kobj = kobject_create_and_add("usbcss_hs", kernel_kobj);
  1271. if (!kobj) {
  1272. dev_err(wcd939x->dev, "%s: Could not create the USBC-SS HS kobj.\n", __func__);
  1273. return -ENOMEM;
  1274. }
  1275. for (i = 0; i < ARRAY_SIZE(usbcss_sysfs_files); i++) {
  1276. rc = create_sysfs_entry_file(wcd939x, usbcss_sysfs_files[i],
  1277. 0644, i, kobj);
  1278. }
  1279. return 0;
  1280. }
  1281. static void wcd939x_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl, uint32_t *zr)
  1282. {
  1283. struct snd_soc_component *component = mbhc->component;
  1284. struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
  1285. struct wcd939x_pdata *pdata = dev_get_platdata(wcd939x->dev);
  1286. s16 reg0, reg1, reg2, reg3, reg4;
  1287. uint32_t zdiff_val = 0, r_gnd_int_fet_mohms = 0, rl_eff_l_mohms = 0, rl_eff_r_mohms = 0;
  1288. uint32_t r_gnd_ext_fet_mohms = 0, r_aud_ext_fet_mohms = 0, r_gnd_res_tot_mohms = 0;
  1289. uint32_t r_gnd_par_tot_mohms = 0, r_gnd_par_route1_mohms = 0;
  1290. uint32_t aud_tap = LINEARIZER_DEFAULT_TAP, zdiff_counter = 0, zdiff_sum = 0;
  1291. uint32_t *zdiff = &zdiff_val;
  1292. s32 z_L_R_delta_mohms = 0;
  1293. int32_t z1L, z1R, z1Ls, z1Diff;
  1294. int zMono, z_diff1, z_diff2;
  1295. size_t i;
  1296. bool is_fsm_disable = false, calculate_lin_aud_tap = false, gnd_ext_fet_updated = false;
  1297. struct wcd939x_mbhc_zdet_param zdet_param = {4, 0, 6, 0x18, 0x60, 0x78};
  1298. struct wcd939x_mbhc_zdet_param *zdet_param_ptr = &zdet_param;
  1299. s16 d1[] = {0, 30, 30, 6};
  1300. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1301. uint32_t cached_regs[4][2] = {{WCD_USBSS_EXT_LIN_EN, 0}, {WCD_USBSS_EXT_SW_CTRL_1, 0},
  1302. {WCD_USBSS_MG1_BIAS, 0}, {WCD_USBSS_MG2_BIAS, 0}};
  1303. uint32_t l_3_6V_regs[4][2] = {{WCD_USBSS_EXT_LIN_EN, 0x00}, {WCD_USBSS_EXT_SW_CTRL_1, 0x00},
  1304. {WCD_USBSS_MG1_BIAS, 0x0E}, {WCD_USBSS_MG2_BIAS, 0x0E}};
  1305. uint32_t diff_regs[2][2] = {{WCD_USBSS_EXT_LIN_EN, 0x00}, {WCD_USBSS_EXT_SW_CTRL_1, 0x00}};
  1306. #endif
  1307. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  1308. /* Turn on RX supplies */
  1309. if (wcd939x->version == WCD939X_VERSION_2_0) {
  1310. /* Start up Buck/Flyback, Enable RX bias, Use MBHC RCO for MBHC Zdet, Enable Vneg */
  1311. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x4E, 0x4E);
  1312. /* Wait 100us for settling */
  1313. usleep_range(100, 110);
  1314. /* Enable VNEGDAC_LDO */
  1315. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x10, 0x10);
  1316. /* Wait 100us for settling */
  1317. usleep_range(100, 110);
  1318. /* Keep PA left/right channels disabled */
  1319. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x01, 0x01);
  1320. /* Enable VPOS */
  1321. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x20, 0x20);
  1322. /* Wait 500us for settling */
  1323. usleep_range(500, 510);
  1324. }
  1325. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1326. /* Cache relevant USB-SS registers */
  1327. wcd_usbss_register_update(cached_regs, WCD_USBSS_READ, ARRAY_SIZE(cached_regs));
  1328. /* Disable 2k pulldown on MG for improved measurement */
  1329. wcd_usbss_register_update(l_3_6V_regs, WCD_USBSS_WRITE, ARRAY_SIZE(l_3_6V_regs));
  1330. #endif
  1331. /* Store register values */
  1332. reg0 = snd_soc_component_read(component, WCD939X_MBHC_BTN5);
  1333. reg1 = snd_soc_component_read(component, WCD939X_MBHC_BTN6);
  1334. reg2 = snd_soc_component_read(component, WCD939X_MBHC_BTN7);
  1335. reg3 = snd_soc_component_read(component, WCD939X_CTL_CLK);
  1336. reg4 = snd_soc_component_read(component, WCD939X_ZDET_ANA_CTL);
  1337. /* Disable the detection FSM */
  1338. if (snd_soc_component_read(component, WCD939X_MBHC_ELECT) & 0x80) {
  1339. is_fsm_disable = true;
  1340. regmap_update_bits(wcd939x->regmap,
  1341. WCD939X_MBHC_ELECT, 0x80, 0x00);
  1342. }
  1343. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  1344. if (mbhc->hphl_swh)
  1345. regmap_update_bits(wcd939x->regmap,
  1346. WCD939X_MBHC_MECH, 0x80, 0x00);
  1347. /* Turn off 100k pull down on HPHL */
  1348. regmap_update_bits(wcd939x->regmap,
  1349. WCD939X_MBHC_MECH, 0x01, 0x00);
  1350. /* Disable surge protection before impedance detection.
  1351. * This is done to give correct value for high impedance.
  1352. */
  1353. regmap_update_bits(wcd939x->regmap,
  1354. WCD939X_HPHLR_SURGE_EN, 0xC0, 0x00);
  1355. /* 1ms delay needed after disable surge protection */
  1356. usleep_range(1000, 1010);
  1357. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1358. /* Disable sense switch and MIC for USB-C analog platforms */
  1359. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1360. wcd_usbss_set_switch_settings_enable(SENSE_SWITCHES, USBSS_SWITCH_DISABLE);
  1361. wcd_usbss_set_switch_settings_enable(MIC_SWITCHES, USBSS_SWITCH_DISABLE);
  1362. }
  1363. #endif
  1364. /* L-channel impedance */
  1365. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
  1366. if ((z1L == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1L > WCD939X_ZDET_VAL_100K)) {
  1367. *zl = WCD939X_ZDET_FLOATING_IMPEDANCE;
  1368. } else {
  1369. *zl = z1L;
  1370. wcd939x_wcd_mbhc_qfuse_cal(component, zl, 0);
  1371. dev_dbg(component->dev, "%s: Calibrated left SE measurement is %d(mohms)\n",
  1372. __func__, *zl);
  1373. apply_zdet_correction(zl, ZDET_SE, pdata->usbcss_hs.se_slope_factor_times_1000,
  1374. pdata->usbcss_hs.diff_slope_factor_times_1000);
  1375. }
  1376. pdata->usbcss_hs.aud.l.zval = *zl;
  1377. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1378. dev_dbg(component->dev,
  1379. "%s: Calibrated and adjusted left SE measurement is %d(mohms)\n", __func__,
  1380. *zl);
  1381. } else {
  1382. dev_dbg(component->dev, "%s: impedance on HPH_L = %d(mohms)\n",
  1383. __func__, *zl);
  1384. }
  1385. /* R-channel impedance */
  1386. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
  1387. if ((z1R == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1R > WCD939X_ZDET_VAL_100K)) {
  1388. *zr = WCD939X_ZDET_FLOATING_IMPEDANCE;
  1389. } else {
  1390. *zr = z1R;
  1391. wcd939x_wcd_mbhc_qfuse_cal(component, zr, 4);
  1392. dev_dbg(component->dev, "%s: Calibrated right SE measurement is %d(mohms)\n",
  1393. __func__, *zr);
  1394. apply_zdet_correction(zr, ZDET_SE, pdata->usbcss_hs.se_slope_factor_times_1000,
  1395. pdata->usbcss_hs.diff_slope_factor_times_1000);
  1396. }
  1397. pdata->usbcss_hs.aud.r.zval = *zr;
  1398. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1399. dev_dbg(component->dev,
  1400. "%s: Calibrated and adjusted right SE measurement is %d(mohms)\n", __func__,
  1401. *zr);
  1402. } else {
  1403. dev_dbg(component->dev, "%s: impedance on HPH_R = %d(mohms)\n",
  1404. __func__, *zr);
  1405. /* Convert from mohms to ohms (rounded) */
  1406. *zl = (*zl + OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  1407. *zr = (*zr + OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  1408. goto mono_stereo_detection;
  1409. }
  1410. /* Differential measurement L to R */
  1411. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1412. /* Disable AGND switch */
  1413. wcd_usbss_set_switch_settings_enable(AGND_SWITCHES, USBSS_SWITCH_DISABLE);
  1414. wcd_usbss_register_update(diff_regs, WCD_USBSS_WRITE, ARRAY_SIZE(diff_regs));
  1415. #endif
  1416. /* Enable HPHR NCLAMP */
  1417. regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_MISC1, 0x08, 0x08);
  1418. /* Wait 3ms for settling */
  1419. usleep_range(3000, 3010);
  1420. /* Differential impedance */
  1421. for (i = 0; i < NUM_DIFF_MEAS; i++) {
  1422. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1Diff, NULL, d1);
  1423. if ((z1Diff == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
  1424. (z1Diff > WCD939X_ZDET_VAL_100K)) {
  1425. } else {
  1426. *zdiff = z1Diff;
  1427. wcd939x_wcd_mbhc_qfuse_cal(component, zdiff, 0);
  1428. dev_dbg(component->dev,
  1429. "%s: Calibrated differential measurement %d is %d(mohms)\n",
  1430. __func__, i + 1, *zdiff);
  1431. apply_zdet_correction(zdiff, ZDET_DIFF,
  1432. pdata->usbcss_hs.se_slope_factor_times_1000,
  1433. pdata->usbcss_hs.diff_slope_factor_times_1000);
  1434. zdiff_sum += *zdiff;
  1435. zdiff_counter++;
  1436. }
  1437. dev_dbg(component->dev,
  1438. "%s: Calibrated and adjusted differential measurement %d is %d(mohms)\n",
  1439. __func__, i + 1, *zdiff);
  1440. }
  1441. /* Take average of measurements */
  1442. if (zdiff_counter == 0)
  1443. *zdiff = WCD939X_ZDET_FLOATING_IMPEDANCE;
  1444. else
  1445. *zdiff = zdiff_sum / zdiff_counter;
  1446. /* Store the average of the measurements */
  1447. pdata->usbcss_hs.zdiffval = *zdiff;
  1448. dev_dbg(component->dev, "%s: %s %d(mohms)\n", __func__,
  1449. "Average of the calibrated and adjusted differential measurement(s) is", *zdiff);
  1450. /* Disable HPHR NCLAMP */
  1451. regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_MISC1, 0x08, 0x00);
  1452. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1453. /* Enable AGND switch */
  1454. wcd_usbss_set_switch_settings_enable(AGND_SWITCHES, USBSS_SWITCH_ENABLE);
  1455. /* Get ground internal resistance based on orientation */
  1456. if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
  1457. r_gnd_int_fet_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_int_fet_mohms;
  1458. r_gnd_par_route1_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_par_route1_mohms;
  1459. r_gnd_par_tot_mohms = pdata->usbcss_hs.gnd.sbu2.r_gnd_par_tot_mohms;
  1460. } else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
  1461. r_gnd_int_fet_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_int_fet_mohms;
  1462. r_gnd_par_route1_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_par_route1_mohms;
  1463. r_gnd_par_tot_mohms = pdata->usbcss_hs.gnd.sbu1.r_gnd_par_tot_mohms;
  1464. } else {
  1465. dev_dbg(component->dev, "%s: Invalid SBU switch orientation\n", __func__);
  1466. *zl = 0;
  1467. *zr = 0;
  1468. goto default_vals;
  1469. }
  1470. #endif
  1471. z_L_R_delta_mohms = *zl - *zr;
  1472. dev_dbg(component->dev, "%s: %s : %d mohms\n", __func__,
  1473. "The difference between the L and R SE measurements (L - R) is", z_L_R_delta_mohms);
  1474. /* Ground path resistance */
  1475. /* Use DTSI params for high zdet SE measurements */
  1476. if (pdata->usbcss_hs.aud.l.zval > ZDET_ACC_LMT_MOHMS ||
  1477. pdata->usbcss_hs.aud.r.zval > ZDET_ACC_LMT_MOHMS) {
  1478. r_gnd_res_tot_mohms = pdata->usbcss_hs.gnd.rdson_mohms + r_gnd_par_tot_mohms +
  1479. r_gnd_int_fet_mohms;
  1480. pdata->usbcss_hs.gnd.r_common_gnd_mohms = r_gnd_res_tot_mohms -
  1481. r_gnd_int_fet_mohms -
  1482. r_gnd_par_route1_mohms;
  1483. dev_dbg(component->dev, "%s: %s %d %s\n", __func__,
  1484. "The r_common_gnd value determined by DTSI parameters,",
  1485. pdata->usbcss_hs.gnd.r_common_gnd_mohms,
  1486. "mohms, is being used instead of calculating r_common_gnd");
  1487. calculate_lin_aud_tap = false;
  1488. } else {
  1489. r_gnd_res_tot_mohms = (*zl + *zr - *zdiff + pdata->usbcss_hs.aud.r_surge_mohms) / 2;
  1490. /* Offset to account for using 3.6V SE measurements */
  1491. r_gnd_res_tot_mohms = (pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms >= 0) ?
  1492. r_gnd_res_tot_mohms -
  1493. (u32) (s32) pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms :
  1494. r_gnd_res_tot_mohms +
  1495. (u32) (s32) (-1 * pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms);
  1496. /* Compute r_common_gnd */
  1497. get_r_common_gnd(wcd939x, r_gnd_res_tot_mohms, r_gnd_int_fet_mohms,
  1498. r_gnd_par_route1_mohms);
  1499. /* Re-calculate ground path resistance based on r_common_gnd */
  1500. r_gnd_res_tot_mohms = pdata->usbcss_hs.gnd.r_common_gnd_mohms +
  1501. r_gnd_int_fet_mohms + r_gnd_par_route1_mohms;
  1502. calculate_lin_aud_tap = true;
  1503. }
  1504. dev_dbg(component->dev, "%s: r_gnd_res_tot_mohms is : %d mohms\n", __func__,
  1505. r_gnd_res_tot_mohms);
  1506. /* Print r_common_gnd buffer */
  1507. for (i = 0; i < R_COMMON_GND_BUFFER_SIZE; i++) {
  1508. dev_dbg(component->dev, "%s: Element %d in r_common_gnd_buffer is : %d mohms\n",
  1509. __func__, i + 1, pdata->usbcss_hs.gnd.r_cm_gnd_buffer.data[i]);
  1510. }
  1511. /* Apply r_common_gnd offset */
  1512. pdata->usbcss_hs.gnd.r_common_gnd_mohms =
  1513. (pdata->usbcss_hs.gnd.r_common_gnd_offset >= 0) ?
  1514. pdata->usbcss_hs.gnd.r_common_gnd_mohms +
  1515. (u32) pdata->usbcss_hs.gnd.r_common_gnd_offset :
  1516. pdata->usbcss_hs.gnd.r_common_gnd_mohms -
  1517. (u32) (-1 * pdata->usbcss_hs.gnd.r_common_gnd_offset);
  1518. /* Ground external FET */
  1519. r_gnd_ext_fet_mohms = r_gnd_res_tot_mohms - r_gnd_par_tot_mohms - r_gnd_int_fet_mohms;
  1520. dev_dbg(component->dev, "%s: r_gnd_ext_fet_mohms is : %d mohms\n", __func__,
  1521. r_gnd_ext_fet_mohms);
  1522. /* Audio external FET */
  1523. r_aud_ext_fet_mohms = (pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms >= 0) ?
  1524. r_gnd_ext_fet_mohms + (u32) (s32) pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms :
  1525. r_gnd_ext_fet_mohms - (u32) (s32) (-1 * pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms);
  1526. dev_dbg(component->dev, "%s: r_aud_ext_fet_mohms is : %d mohms\n", __func__,
  1527. r_aud_ext_fet_mohms);
  1528. /* Compute effective load resistance */
  1529. rl_eff_l_mohms = *zl - pdata->usbcss_hs.aud.l.r_aud_int_fet_mohms - r_aud_ext_fet_mohms -
  1530. r_gnd_res_tot_mohms;
  1531. rl_eff_r_mohms = *zr - pdata->usbcss_hs.aud.r.r_aud_int_fet_mohms - r_aud_ext_fet_mohms -
  1532. r_gnd_res_tot_mohms;
  1533. /* Store z values */
  1534. *zl = (rl_eff_l_mohms - R_CONN_PAR_LOAD_POS_MOHMS - pdata->usbcss_hs.aud.l.r3 +
  1535. OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  1536. dev_dbg(component->dev, "%s: rload_l is : %d mohms\n", __func__,
  1537. rl_eff_l_mohms - R_CONN_PAR_LOAD_POS_MOHMS - pdata->usbcss_hs.aud.l.r3);
  1538. *zr = (rl_eff_r_mohms - R_CONN_PAR_LOAD_POS_MOHMS - pdata->usbcss_hs.aud.r.r3 +
  1539. OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
  1540. dev_dbg(component->dev, "%s: rload_r is : %d mohms\n", __func__,
  1541. rl_eff_r_mohms - R_CONN_PAR_LOAD_POS_MOHMS - pdata->usbcss_hs.aud.r.r3);
  1542. /* Check bounds on effective load values and store the value */
  1543. if (rl_eff_l_mohms > MAX_RL_EFF_MOHMS)
  1544. rl_eff_l_mohms = MAX_RL_EFF_MOHMS;
  1545. else if (rl_eff_l_mohms < MIN_RL_EFF_MOHMS)
  1546. rl_eff_l_mohms = MIN_RL_EFF_MOHMS;
  1547. pdata->usbcss_hs.aud.l.r_load_eff_mohms = rl_eff_l_mohms;
  1548. if (rl_eff_r_mohms > MAX_RL_EFF_MOHMS)
  1549. rl_eff_r_mohms = MAX_RL_EFF_MOHMS;
  1550. else if (rl_eff_r_mohms < MIN_RL_EFF_MOHMS)
  1551. rl_eff_r_mohms = MIN_RL_EFF_MOHMS;
  1552. pdata->usbcss_hs.aud.r.r_load_eff_mohms = rl_eff_r_mohms;
  1553. /* Update FET values and resistances */
  1554. update_ext_fet_res(pdata, r_aud_ext_fet_mohms, r_gnd_ext_fet_mohms);
  1555. /* Update xtalk params */
  1556. /* For SE measurements greater than ZDET_SE_MAX_MOHMS, use default xtalk values */
  1557. if (pdata->usbcss_hs.aud.l.zval > ZDET_SE_MAX_MOHMS ||
  1558. pdata->usbcss_hs.aud.r.zval > ZDET_SE_MAX_MOHMS) {
  1559. pdata->usbcss_hs.xtalk.scale_l = MAX_XTALK_SCALE;
  1560. pdata->usbcss_hs.xtalk.scale_r = MAX_XTALK_SCALE;
  1561. pdata->usbcss_hs.xtalk.alpha_l = MIN_XTALK_ALPHA;
  1562. pdata->usbcss_hs.xtalk.alpha_r = MIN_XTALK_ALPHA;
  1563. dev_dbg(component->dev, "%s: %s %d, %s\n",
  1564. __func__, "The SE zdet measurement is greater than ZDET_SE_MAX_MOHMS,",
  1565. ZDET_SE_MAX_MOHMS,
  1566. "so the default xtalk scale and alpha values will be used");
  1567. } else {
  1568. update_xtalk_scale_and_alpha(wcd939x);
  1569. /* Compute updated linearizer tap */
  1570. if (calculate_lin_aud_tap) {
  1571. if (r_gnd_ext_fet_mohms < pdata->usbcss_hs.gnd.gnd_ext_fet_min_mohms) {
  1572. r_gnd_ext_fet_mohms = pdata->usbcss_hs.gnd.gnd_ext_fet_min_mohms;
  1573. gnd_ext_fet_updated = true;
  1574. }
  1575. if (r_gnd_ext_fet_mohms > GND_EXT_FET_MAX_MOHMS) {
  1576. r_gnd_ext_fet_mohms = GND_EXT_FET_MAX_MOHMS;
  1577. gnd_ext_fet_updated = true;
  1578. }
  1579. if (gnd_ext_fet_updated) {
  1580. dev_dbg(component->dev, "%s: %s %d mohms\n", __func__,
  1581. "Updated (for linearizer) r_gnd_ext_fet_mohms is :",
  1582. r_gnd_ext_fet_mohms);
  1583. /* Audio external FET */
  1584. r_aud_ext_fet_mohms =
  1585. (pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms >= 0) ?
  1586. r_gnd_ext_fet_mohms +
  1587. (u32) (s32) pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms :
  1588. r_gnd_ext_fet_mohms -
  1589. (u32) (s32) (-1 * pdata->usbcss_hs.gnd.gnd_ext_fet_delta_mohms);
  1590. dev_dbg(component->dev, "%s: %s %d mohms\n", __func__,
  1591. "Updated (for linearizer) r_aud_ext_fet_mohms is :",
  1592. r_aud_ext_fet_mohms);
  1593. /* Update FET values and resistances */
  1594. update_ext_fet_res(pdata, r_aud_ext_fet_mohms, r_gnd_ext_fet_mohms);
  1595. }
  1596. get_linearizer_taps(pdata, &aud_tap);
  1597. }
  1598. }
  1599. /* Print xtalk params */
  1600. dev_dbg(component->dev, "%s: Left-channel: Xtalk scale is 0x%x and alpha is 0x%x\n",
  1601. __func__, pdata->usbcss_hs.xtalk.scale_l, pdata->usbcss_hs.xtalk.alpha_l);
  1602. dev_dbg(component->dev, "%s: Right-channel: Xtalk scale is 0x%x and alpha is 0x%x\n",
  1603. __func__, pdata->usbcss_hs.xtalk.scale_r, pdata->usbcss_hs.xtalk.alpha_r);
  1604. /* Revert r_common_gnd offset */
  1605. pdata->usbcss_hs.gnd.r_common_gnd_mohms = (pdata->usbcss_hs.gnd.r_common_gnd_offset >= 0) ?
  1606. pdata->usbcss_hs.gnd.r_common_gnd_mohms -
  1607. (u32) pdata->usbcss_hs.gnd.r_common_gnd_offset :
  1608. pdata->usbcss_hs.gnd.r_common_gnd_mohms +
  1609. (u32) (-1 * pdata->usbcss_hs.gnd.r_common_gnd_offset);
  1610. mono_stereo_detection:
  1611. /* Mono/stereo detection */
  1612. if ((*zl == WCD939X_ZDET_FLOATING_IMPEDANCE) && (*zr == WCD939X_ZDET_FLOATING_IMPEDANCE)) {
  1613. dev_dbg(component->dev,
  1614. "%s: plug type is invalid or extension cable\n",
  1615. __func__);
  1616. goto zdet_complete;
  1617. }
  1618. if ((*zl == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
  1619. (*zr == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
  1620. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  1621. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  1622. dev_dbg(component->dev,
  1623. "%s: Mono plug type with one ch floating or shorted to GND\n",
  1624. __func__);
  1625. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  1626. goto zdet_complete;
  1627. }
  1628. snd_soc_component_update_bits(component, WCD939X_R_ATEST, 0x02, 0x02);
  1629. snd_soc_component_update_bits(component, WCD939X_PA_CTL2, 0x40, 0x01);
  1630. wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1Ls, NULL, d1);
  1631. snd_soc_component_update_bits(component, WCD939X_PA_CTL2, 0x40, 0x00);
  1632. snd_soc_component_update_bits(component, WCD939X_R_ATEST, 0x02, 0x00);
  1633. z1Ls /= 1000;
  1634. wcd939x_wcd_mbhc_qfuse_cal(component, &z1Ls, 0);
  1635. /* Parallel of left Z and 9 ohm pull down resistor */
  1636. zMono = ((*zl) * 9) / ((*zl) + 9);
  1637. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  1638. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  1639. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  1640. dev_dbg(component->dev, "%s: stereo plug type detected\n",
  1641. __func__);
  1642. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  1643. } else {
  1644. dev_dbg(component->dev, "%s: MONO plug type detected\n",
  1645. __func__);
  1646. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  1647. }
  1648. goto zdet_complete;
  1649. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1650. default_vals:
  1651. pdata->usbcss_hs.xtalk.scale_l = MAX_XTALK_SCALE;
  1652. pdata->usbcss_hs.xtalk.scale_r = MAX_XTALK_SCALE;
  1653. pdata->usbcss_hs.xtalk.alpha_l = MIN_XTALK_ALPHA;
  1654. pdata->usbcss_hs.xtalk.alpha_r = MIN_XTALK_ALPHA;
  1655. /* Print xtalk params */
  1656. dev_dbg(component->dev,
  1657. "%s: Left-channel: Xtalk scale is 0x%x and alpha is 0x%x\n", __func__,
  1658. pdata->usbcss_hs.xtalk.scale_l, pdata->usbcss_hs.xtalk.alpha_l);
  1659. dev_dbg(component->dev,
  1660. "%s: Right-channel: Xtalk scale is 0x%x and alpha is 0x%x\n", __func__,
  1661. pdata->usbcss_hs.xtalk.scale_r, pdata->usbcss_hs.xtalk.alpha_r);
  1662. #endif
  1663. zdet_complete:
  1664. /* Configure linearizer */
  1665. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1666. wcd_usbss_set_linearizer_sw_tap(aud_tap, LINEARIZER_DEFAULT_TAP);
  1667. #endif
  1668. /* Print linearizer values */
  1669. dev_dbg(component->dev, "%s: Linearizer aud_tap is 0x%x\n",
  1670. __func__, aud_tap);
  1671. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1672. /* Enable sense switch and MIC for USB-C analog platforms */
  1673. if (mbhc->mbhc_cfg->enable_usbc_analog) {
  1674. wcd_usbss_set_switch_settings_enable(SENSE_SWITCHES, USBSS_SWITCH_ENABLE);
  1675. wcd_usbss_set_switch_settings_enable(MIC_SWITCHES, USBSS_SWITCH_ENABLE);
  1676. }
  1677. #endif
  1678. /* Enable surge protection again after impedance detection for platforms other than USB-C
  1679. * analog platforms
  1680. */
  1681. if (!(mbhc->mbhc_cfg->enable_usbc_analog))
  1682. regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_EN, 0xC0, 0xC0);
  1683. snd_soc_component_write(component, WCD939X_MBHC_BTN5, reg0);
  1684. snd_soc_component_write(component, WCD939X_MBHC_BTN6, reg1);
  1685. snd_soc_component_write(component, WCD939X_MBHC_BTN7, reg2);
  1686. /* Turn on 100k pull down on HPHL */
  1687. regmap_update_bits(wcd939x->regmap,
  1688. WCD939X_MBHC_MECH, 0x01, 0x01);
  1689. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  1690. if (mbhc->hphl_swh)
  1691. regmap_update_bits(wcd939x->regmap,
  1692. WCD939X_MBHC_MECH, 0x80, 0x80);
  1693. snd_soc_component_write(component, WCD939X_ZDET_ANA_CTL, reg4);
  1694. snd_soc_component_write(component, WCD939X_CTL_CLK, reg3);
  1695. if (is_fsm_disable)
  1696. regmap_update_bits(wcd939x->regmap,
  1697. WCD939X_MBHC_ELECT, 0x80, 0x80);
  1698. #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
  1699. wcd_usbss_register_update(cached_regs, WCD_USBSS_WRITE, ARRAY_SIZE(cached_regs));
  1700. #endif
  1701. /* Turn off RX supplies */
  1702. if (wcd939x->version == WCD939X_VERSION_2_0) {
  1703. /* Set VPOS to be controlled by RX */
  1704. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x20, 0x00);
  1705. /* Wait 500us for settling */
  1706. usleep_range(500, 510);
  1707. /* Set PA Left/Right channels and VNEGDAC_LDO to be controlled by RX */
  1708. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x11, 0x00);
  1709. /* Wait 100us for settling */
  1710. usleep_range(100, 110);
  1711. /* Set Vneg mode and enable to be controlled by RX */
  1712. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x06, 0x00);
  1713. /* Wait 100us for settling */
  1714. usleep_range(100, 110);
  1715. /* Set RX bias to be controlled by RX and set Buck/Flyback back to SWR Rx clock */
  1716. regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x48, 0x00);
  1717. }
  1718. }
  1719. static void wcd939x_mbhc_gnd_det_ctrl(struct snd_soc_component *component,
  1720. bool enable)
  1721. {
  1722. if (enable) {
  1723. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1724. 0x02, 0x02);
  1725. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1726. 0x40, 0x40);
  1727. } else {
  1728. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1729. 0x40, 0x00);
  1730. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  1731. 0x02, 0x00);
  1732. }
  1733. }
  1734. static void wcd939x_mbhc_hph_pull_down_ctrl(struct snd_soc_component *component,
  1735. bool enable)
  1736. {
  1737. if (enable) {
  1738. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1739. 0x40, 0x40);
  1740. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1741. 0x10, 0x10);
  1742. } else {
  1743. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1744. 0x40, 0x00);
  1745. snd_soc_component_update_bits(component, WCD939X_PA_CTL2,
  1746. 0x10, 0x00);
  1747. }
  1748. }
  1749. static void wcd939x_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  1750. {
  1751. struct snd_soc_component *component = mbhc->component;
  1752. if ((mbhc->moist_rref == R_OFF) ||
  1753. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  1754. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1755. 0x0C, R_OFF << 2);
  1756. return;
  1757. }
  1758. /* Do not enable moisture detection if jack type is NC */
  1759. if (!mbhc->hphl_swh) {
  1760. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  1761. __func__);
  1762. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1763. 0x0C, R_OFF << 2);
  1764. return;
  1765. }
  1766. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1767. 0x0C, mbhc->moist_rref << 2);
  1768. }
  1769. static void wcd939x_mbhc_moisture_detect_en(struct wcd_mbhc *mbhc, bool enable)
  1770. {
  1771. struct snd_soc_component *component = mbhc->component;
  1772. if (enable)
  1773. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1774. 0x0C, mbhc->moist_rref << 2);
  1775. else
  1776. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1777. 0x0C, R_OFF << 2);
  1778. }
  1779. static bool wcd939x_mbhc_get_moisture_status(struct wcd_mbhc *mbhc)
  1780. {
  1781. struct snd_soc_component *component = mbhc->component;
  1782. bool ret = false;
  1783. if ((mbhc->moist_rref == R_OFF) ||
  1784. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  1785. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1786. 0x0C, R_OFF << 2);
  1787. goto done;
  1788. }
  1789. /* Do not enable moisture detection if jack type is NC */
  1790. if (!mbhc->hphl_swh) {
  1791. dev_dbg(component->dev, "%s: disable moisture detection for NC\n",
  1792. __func__);
  1793. snd_soc_component_update_bits(component, WCD939X_CTL_2,
  1794. 0x0C, R_OFF << 2);
  1795. goto done;
  1796. }
  1797. /*
  1798. * If moisture_en is already enabled, then skip to plug type
  1799. * detection.
  1800. */
  1801. if ((snd_soc_component_read(component, WCD939X_CTL_2) & 0x0C))
  1802. goto done;
  1803. wcd939x_mbhc_moisture_detect_en(mbhc, true);
  1804. /* Read moisture comparator status */
  1805. ret = ((snd_soc_component_read(component, WCD939X_FSM_STATUS)
  1806. & 0x20) ? 0 : 1);
  1807. done:
  1808. return ret;
  1809. }
  1810. static void wcd939x_mbhc_moisture_polling_ctrl(struct wcd_mbhc *mbhc,
  1811. bool enable)
  1812. {
  1813. struct snd_soc_component *component = mbhc->component;
  1814. snd_soc_component_update_bits(component,
  1815. WCD939X_MOISTURE_DET_POLLING_CTRL,
  1816. 0x04, (enable << 2));
  1817. }
  1818. static void wcd939x_mbhc_bcs_enable(struct wcd_mbhc *mbhc,
  1819. bool bcs_enable)
  1820. {
  1821. if (bcs_enable)
  1822. wcd939x_disable_bcs_before_slow_insert(mbhc->component, false);
  1823. else
  1824. wcd939x_disable_bcs_before_slow_insert(mbhc->component, true);
  1825. }
  1826. static void wcd939x_surge_reset_routine(struct wcd_mbhc *mbhc)
  1827. {
  1828. struct wcd939x_priv *wcd939x = snd_soc_component_get_drvdata(mbhc->component);
  1829. regcache_mark_dirty(wcd939x->regmap);
  1830. regcache_sync(wcd939x->regmap);
  1831. }
  1832. static void wcd939x_mbhc_zdet_leakage_resistance(struct wcd_mbhc *mbhc,
  1833. bool enable)
  1834. {
  1835. if (enable)
  1836. snd_soc_component_update_bits(mbhc->component, WCD939X_ZDET_BIAS_CTL,
  1837. 0x80, 0x80); /* disable 1M pull-up */
  1838. else
  1839. snd_soc_component_update_bits(mbhc->component, WCD939X_ZDET_BIAS_CTL,
  1840. 0x80, 0x00); /* enable 1M pull-up */
  1841. }
  1842. static const struct wcd_mbhc_cb mbhc_cb = {
  1843. .request_irq = wcd939x_mbhc_request_irq,
  1844. .irq_control = wcd939x_mbhc_irq_control,
  1845. .free_irq = wcd939x_mbhc_free_irq,
  1846. .clk_setup = wcd939x_mbhc_clk_setup,
  1847. .map_btn_code_to_num = wcd939x_mbhc_btn_to_num,
  1848. .mbhc_bias = wcd939x_mbhc_mbhc_bias_control,
  1849. .set_btn_thr = wcd939x_mbhc_program_btn_thr,
  1850. .lock_sleep = wcd939x_mbhc_lock_sleep,
  1851. .register_notifier = wcd939x_mbhc_register_notifier,
  1852. .micbias_enable_status = wcd939x_mbhc_micb_en_status,
  1853. .hph_pa_on_status = wcd939x_mbhc_hph_pa_on_status,
  1854. .hph_pull_up_control_v2 = wcd939x_mbhc_hph_l_pull_up_control,
  1855. .mbhc_micbias_control = wcd939x_mbhc_request_micbias,
  1856. .mbhc_micb_ramp_control = wcd939x_mbhc_micb_ramp_control,
  1857. .get_hwdep_fw_cal = wcd939x_get_hwdep_fw_cal,
  1858. .mbhc_micb_ctrl_thr_mic = wcd939x_mbhc_micb_ctrl_threshold_mic,
  1859. .compute_impedance = wcd939x_wcd_mbhc_calc_impedance,
  1860. .mbhc_gnd_det_ctrl = wcd939x_mbhc_gnd_det_ctrl,
  1861. .hph_pull_down_ctrl = wcd939x_mbhc_hph_pull_down_ctrl,
  1862. .mbhc_moisture_config = wcd939x_mbhc_moisture_config,
  1863. .mbhc_get_moisture_status = wcd939x_mbhc_get_moisture_status,
  1864. .mbhc_moisture_polling_ctrl = wcd939x_mbhc_moisture_polling_ctrl,
  1865. .mbhc_moisture_detect_en = wcd939x_mbhc_moisture_detect_en,
  1866. .bcs_enable = wcd939x_mbhc_bcs_enable,
  1867. .surge_reset_routine = wcd939x_surge_reset_routine,
  1868. .zdet_leakage_resistance = wcd939x_mbhc_zdet_leakage_resistance,
  1869. };
  1870. static int wcd939x_get_hph_type(struct snd_kcontrol *kcontrol,
  1871. struct snd_ctl_elem_value *ucontrol)
  1872. {
  1873. struct snd_soc_component *component =
  1874. snd_soc_kcontrol_component(kcontrol);
  1875. struct wcd939x_mbhc *wcd939x_mbhc = wcd939x_soc_get_mbhc(component);
  1876. struct wcd_mbhc *mbhc;
  1877. if (!wcd939x_mbhc) {
  1878. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1879. return -EINVAL;
  1880. }
  1881. mbhc = &wcd939x_mbhc->wcd_mbhc;
  1882. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  1883. dev_dbg(component->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  1884. return 0;
  1885. }
  1886. static int wcd939x_hph_impedance_get(struct snd_kcontrol *kcontrol,
  1887. struct snd_ctl_elem_value *ucontrol)
  1888. {
  1889. uint32_t zl, zr;
  1890. bool hphr;
  1891. struct soc_multi_mixer_control *mc;
  1892. struct snd_soc_component *component =
  1893. snd_soc_kcontrol_component(kcontrol);
  1894. struct wcd939x_mbhc *wcd939x_mbhc = wcd939x_soc_get_mbhc(component);
  1895. if (!wcd939x_mbhc) {
  1896. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1897. return -EINVAL;
  1898. }
  1899. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1900. hphr = mc->shift;
  1901. wcd_mbhc_get_impedance(&wcd939x_mbhc->wcd_mbhc, &zl, &zr);
  1902. dev_dbg(component->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  1903. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  1904. return 0;
  1905. }
  1906. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  1907. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  1908. wcd939x_get_hph_type, NULL),
  1909. };
  1910. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  1911. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  1912. wcd939x_hph_impedance_get, NULL),
  1913. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  1914. wcd939x_hph_impedance_get, NULL),
  1915. };
  1916. /*
  1917. * wcd939x_mbhc_get_impedance: get impedance of headphone
  1918. * left and right channels
  1919. * @wcd939x_mbhc: handle to struct wcd939x_mbhc *
  1920. * @zl: handle to left-ch impedance
  1921. * @zr: handle to right-ch impedance
  1922. * return 0 for success or error code in case of failure
  1923. */
  1924. int wcd939x_mbhc_get_impedance(struct wcd939x_mbhc *wcd939x_mbhc,
  1925. uint32_t *zl, uint32_t *zr)
  1926. {
  1927. if (!wcd939x_mbhc) {
  1928. pr_err_ratelimited("%s: mbhc not initialized!\n", __func__);
  1929. return -EINVAL;
  1930. }
  1931. if (!zl || !zr) {
  1932. pr_err_ratelimited("%s: zl or zr null!\n", __func__);
  1933. return -EINVAL;
  1934. }
  1935. return wcd_mbhc_get_impedance(&wcd939x_mbhc->wcd_mbhc, zl, zr);
  1936. }
  1937. EXPORT_SYMBOL(wcd939x_mbhc_get_impedance);
  1938. /*
  1939. * wcd939x_mbhc_hs_detect: starts mbhc insertion/removal functionality
  1940. * @codec: handle to snd_soc_component *
  1941. * @mbhc_cfg: handle to mbhc configuration structure
  1942. * return 0 if mbhc_start is success or error code in case of failure
  1943. */
  1944. int wcd939x_mbhc_hs_detect(struct snd_soc_component *component,
  1945. struct wcd_mbhc_config *mbhc_cfg)
  1946. {
  1947. struct wcd939x_priv *wcd939x = NULL;
  1948. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  1949. if (!component) {
  1950. pr_err_ratelimited("%s: component is NULL\n", __func__);
  1951. return -EINVAL;
  1952. }
  1953. wcd939x = snd_soc_component_get_drvdata(component);
  1954. if (!wcd939x) {
  1955. pr_err_ratelimited("%s: wcd939x is NULL\n", __func__);
  1956. return -EINVAL;
  1957. }
  1958. wcd939x_mbhc = wcd939x->mbhc;
  1959. if (!wcd939x_mbhc) {
  1960. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1961. return -EINVAL;
  1962. }
  1963. return wcd_mbhc_start(&wcd939x_mbhc->wcd_mbhc, mbhc_cfg);
  1964. }
  1965. EXPORT_SYMBOL(wcd939x_mbhc_hs_detect);
  1966. /*
  1967. * wcd939x_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  1968. * @component: handle to snd_soc_component *
  1969. */
  1970. void wcd939x_mbhc_hs_detect_exit(struct snd_soc_component *component)
  1971. {
  1972. struct wcd939x_priv *wcd939x = NULL;
  1973. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  1974. if (!component) {
  1975. pr_err_ratelimited("%s: component is NULL\n", __func__);
  1976. return;
  1977. }
  1978. wcd939x = snd_soc_component_get_drvdata(component);
  1979. if (!wcd939x) {
  1980. pr_err_ratelimited("%s: wcd939x is NULL\n", __func__);
  1981. return;
  1982. }
  1983. wcd939x_mbhc = wcd939x->mbhc;
  1984. if (!wcd939x_mbhc) {
  1985. dev_err_ratelimited(component->dev, "%s: mbhc not initialized!\n", __func__);
  1986. return;
  1987. }
  1988. wcd_mbhc_stop(&wcd939x_mbhc->wcd_mbhc);
  1989. }
  1990. EXPORT_SYMBOL(wcd939x_mbhc_hs_detect_exit);
  1991. /*
  1992. * wcd939x_mbhc_ssr_down: stop mbhc during
  1993. * wcd939x subsystem restart
  1994. * mbhc: pointer to wcd937x_mbhc structure
  1995. * component: handle to snd_soc_component *
  1996. */
  1997. void wcd939x_mbhc_ssr_down(struct wcd939x_mbhc *mbhc,
  1998. struct snd_soc_component *component)
  1999. {
  2000. struct wcd_mbhc *wcd_mbhc = NULL;
  2001. if (!mbhc || !component)
  2002. return;
  2003. wcd_mbhc = &mbhc->wcd_mbhc;
  2004. if (!wcd_mbhc) {
  2005. dev_err_ratelimited(component->dev, "%s: wcd_mbhc is NULL\n", __func__);
  2006. return;
  2007. }
  2008. wcd939x_mbhc_hs_detect_exit(component);
  2009. wcd_mbhc_deinit(wcd_mbhc);
  2010. }
  2011. EXPORT_SYMBOL(wcd939x_mbhc_ssr_down);
  2012. /*
  2013. * wcd939x_mbhc_post_ssr_init: initialize mbhc for
  2014. * wcd939x post subsystem restart
  2015. * @mbhc: poniter to wcd939x_mbhc structure
  2016. * @component: handle to snd_soc_component *
  2017. *
  2018. * return 0 if mbhc_init is success or error code in case of failure
  2019. */
  2020. int wcd939x_mbhc_post_ssr_init(struct wcd939x_mbhc *mbhc,
  2021. struct snd_soc_component *component)
  2022. {
  2023. int ret = 0;
  2024. struct wcd_mbhc *wcd_mbhc = NULL;
  2025. if (!mbhc || !component)
  2026. return -EINVAL;
  2027. wcd_mbhc = &mbhc->wcd_mbhc;
  2028. if (wcd_mbhc == NULL) {
  2029. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  2030. return -EINVAL;
  2031. }
  2032. /* Reset detection type to insertion after SSR recovery */
  2033. snd_soc_component_update_bits(component, WCD939X_MBHC_MECH,
  2034. 0x20, 0x20);
  2035. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb, &intr_ids,
  2036. wcd_mbhc_registers, WCD939X_ZDET_SUPPORTED);
  2037. if (ret) {
  2038. dev_err(component->dev, "%s: mbhc initialization failed\n",
  2039. __func__);
  2040. goto done;
  2041. }
  2042. done:
  2043. return ret;
  2044. }
  2045. EXPORT_SYMBOL(wcd939x_mbhc_post_ssr_init);
  2046. /*
  2047. * wcd939x_mbhc_init: initialize mbhc for wcd939x
  2048. * @mbhc: poniter to wcd939x_mbhc struct pointer to store the configs
  2049. * @codec: handle to snd_soc_component *
  2050. * @fw_data: handle to firmware data
  2051. *
  2052. * return 0 if mbhc_init is success or error code in case of failure
  2053. */
  2054. int wcd939x_mbhc_init(struct wcd939x_mbhc **mbhc,
  2055. struct snd_soc_component *component,
  2056. struct fw_info *fw_data)
  2057. {
  2058. struct wcd939x_mbhc *wcd939x_mbhc = NULL;
  2059. struct wcd_mbhc *wcd_mbhc = NULL;
  2060. int ret = 0;
  2061. struct wcd939x_pdata *pdata;
  2062. struct wcd939x_priv *wcd939x;
  2063. if (!component) {
  2064. pr_err("%s: component is NULL\n", __func__);
  2065. return -EINVAL;
  2066. }
  2067. wcd939x_mbhc = devm_kzalloc(component->dev, sizeof(struct wcd939x_mbhc),
  2068. GFP_KERNEL);
  2069. if (!wcd939x_mbhc)
  2070. return -ENOMEM;
  2071. wcd939x_mbhc->fw_data = fw_data;
  2072. BLOCKING_INIT_NOTIFIER_HEAD(&wcd939x_mbhc->notifier);
  2073. wcd_mbhc = &wcd939x_mbhc->wcd_mbhc;
  2074. if (wcd_mbhc == NULL) {
  2075. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  2076. ret = -EINVAL;
  2077. goto err;
  2078. }
  2079. /* Setting default mbhc detection logic to ADC */
  2080. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  2081. /* Down ramp timer set-up */
  2082. timer_setup(&wcd939x_mbhc->rdown_timer, rdown_timer_callback, 0);
  2083. wcd939x_mbhc->rdown_prev_iter = 0;
  2084. wcd939x_mbhc->rdown_timer_complete = false;
  2085. pdata = dev_get_platdata(component->dev);
  2086. if (!pdata) {
  2087. dev_err(component->dev, "%s: pdata pointer is NULL\n",
  2088. __func__);
  2089. ret = -EINVAL;
  2090. goto err;
  2091. }
  2092. wcd_mbhc->micb_mv = pdata->micbias.micb2_mv;
  2093. ret = wcd_mbhc_init(wcd_mbhc, component, &mbhc_cb,
  2094. &intr_ids, wcd_mbhc_registers,
  2095. WCD939X_ZDET_SUPPORTED);
  2096. if (ret) {
  2097. dev_err(component->dev, "%s: mbhc initialization failed\n",
  2098. __func__);
  2099. goto err;
  2100. }
  2101. (*mbhc) = wcd939x_mbhc;
  2102. snd_soc_add_component_controls(component, impedance_detect_controls,
  2103. ARRAY_SIZE(impedance_detect_controls));
  2104. snd_soc_add_component_controls(component, hph_type_detect_controls,
  2105. ARRAY_SIZE(hph_type_detect_controls));
  2106. wcd939x = dev_get_drvdata(component->dev);
  2107. if (!wcd939x) {
  2108. dev_err(component->dev, "%s: wcd939x pointer is NULL\n", __func__);
  2109. ret = -EINVAL;
  2110. goto err;
  2111. }
  2112. usbcss_hs_sysfs_init(wcd939x);
  2113. return 0;
  2114. err:
  2115. if (wcd939x_mbhc)
  2116. del_timer(&wcd939x_mbhc->rdown_timer);
  2117. devm_kfree(component->dev, wcd939x_mbhc);
  2118. return ret;
  2119. }
  2120. EXPORT_SYMBOL(wcd939x_mbhc_init);
  2121. /*
  2122. * wcd939x_mbhc_deinit: deinitialize mbhc for wcd939x
  2123. * @codec: handle to snd_soc_component *
  2124. */
  2125. void wcd939x_mbhc_deinit(struct snd_soc_component *component)
  2126. {
  2127. struct wcd939x_priv *wcd939x;
  2128. struct wcd939x_mbhc *wcd939x_mbhc;
  2129. if (!component) {
  2130. pr_err("%s: component is NULL\n", __func__);
  2131. return;
  2132. }
  2133. wcd939x = snd_soc_component_get_drvdata(component);
  2134. if (!wcd939x) {
  2135. pr_err("%s: wcd939x is NULL\n", __func__);
  2136. return;
  2137. }
  2138. wcd939x_mbhc = wcd939x->mbhc;
  2139. if (wcd939x_mbhc) {
  2140. del_timer(&wcd939x_mbhc->rdown_timer);
  2141. wcd_mbhc_deinit(&wcd939x_mbhc->wcd_mbhc);
  2142. devm_kfree(component->dev, wcd939x_mbhc);
  2143. }
  2144. }
  2145. EXPORT_SYMBOL(wcd939x_mbhc_deinit);