wcd939x-mbhc.c 67 KB

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