wcd9xxx-common-v2.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2018, 2020-2021 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <sound/soc.h>
  9. #include <linux/kernel.h>
  10. #include <linux/delay.h>
  11. #include <asoc/core.h>
  12. #include <asoc/wcd9xxx-common-v2.h>
  13. #include <asoc/wcd9xxx_registers.h>
  14. #define WCD_USLEEP_RANGE 50
  15. #define MAX_IMPED_PARAMS 6
  16. enum {
  17. DAC_GAIN_0DB = 0,
  18. DAC_GAIN_0P2DB,
  19. DAC_GAIN_0P4DB,
  20. DAC_GAIN_0P6DB,
  21. DAC_GAIN_0P8DB,
  22. DAC_GAIN_M0P2DB,
  23. DAC_GAIN_M0P4DB,
  24. DAC_GAIN_M0P6DB,
  25. };
  26. enum {
  27. VREF_FILT_R_0OHM = 0,
  28. VREF_FILT_R_25KOHM,
  29. VREF_FILT_R_50KOHM,
  30. VREF_FILT_R_100KOHM,
  31. };
  32. enum {
  33. DELTA_I_0MA,
  34. DELTA_I_10MA,
  35. DELTA_I_20MA,
  36. DELTA_I_30MA,
  37. DELTA_I_40MA,
  38. DELTA_I_50MA,
  39. };
  40. struct wcd_imped_val {
  41. u32 imped_val;
  42. u8 index;
  43. };
  44. static const struct wcd_reg_mask_val imped_table[][MAX_IMPED_PARAMS] = {
  45. {
  46. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xf5},
  47. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf5},
  48. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  49. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xf5},
  50. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf5},
  51. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  52. },
  53. {
  54. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xf7},
  55. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf7},
  56. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  57. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xf7},
  58. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf7},
  59. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  60. },
  61. {
  62. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xf9},
  63. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf9},
  64. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x0},
  65. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xf9},
  66. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf9},
  67. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x0},
  68. },
  69. {
  70. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfa},
  71. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfa},
  72. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  73. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfa},
  74. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfa},
  75. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  76. },
  77. {
  78. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfb},
  79. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfb},
  80. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  81. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfb},
  82. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfb},
  83. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  84. },
  85. {
  86. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfc},
  87. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfc},
  88. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  89. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfc},
  90. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfc},
  91. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  92. },
  93. {
  94. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfd},
  95. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfd},
  96. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  97. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfd},
  98. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfd},
  99. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  100. },
  101. {
  102. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfe},
  103. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfe},
  104. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  105. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfe},
  106. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfe},
  107. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  108. },
  109. {
  110. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xff},
  111. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xff},
  112. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  113. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xff},
  114. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xff},
  115. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  116. },
  117. };
  118. static const struct wcd_reg_mask_val imped_table_tavil[][MAX_IMPED_PARAMS] = {
  119. {
  120. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xf2},
  121. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf2},
  122. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  123. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xf2},
  124. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf2},
  125. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  126. },
  127. {
  128. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xf4},
  129. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf4},
  130. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  131. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xf4},
  132. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf4},
  133. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  134. },
  135. {
  136. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xf7},
  137. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf7},
  138. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  139. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xf7},
  140. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf7},
  141. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  142. },
  143. {
  144. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xf9},
  145. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf9},
  146. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  147. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xf9},
  148. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf9},
  149. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  150. },
  151. {
  152. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfa},
  153. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfa},
  154. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  155. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfa},
  156. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfa},
  157. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  158. },
  159. {
  160. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfb},
  161. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfb},
  162. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  163. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfb},
  164. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfb},
  165. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  166. },
  167. {
  168. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfc},
  169. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfc},
  170. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  171. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfc},
  172. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfc},
  173. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  174. },
  175. {
  176. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfd},
  177. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfd},
  178. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  179. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfd},
  180. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfd},
  181. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  182. },
  183. {
  184. {WCD9XXX_CDC_RX1_RX_VOL_CTL, 0xff, 0xfd},
  185. {WCD9XXX_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfd},
  186. {WCD9XXX_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  187. {WCD9XXX_CDC_RX2_RX_VOL_CTL, 0xff, 0xfd},
  188. {WCD9XXX_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfd},
  189. {WCD9XXX_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  190. },
  191. };
  192. static const struct wcd_imped_val imped_index[] = {
  193. {4, 0},
  194. {5, 1},
  195. {6, 2},
  196. {7, 3},
  197. {8, 4},
  198. {9, 5},
  199. {10, 6},
  200. {11, 7},
  201. {12, 8},
  202. {13, 9},
  203. };
  204. static void (*clsh_state_fp[NUM_CLSH_STATES_V2])(struct snd_soc_component *,
  205. struct wcd_clsh_cdc_data *,
  206. u8 req_state, bool en, int mode);
  207. static int get_impedance_index(int imped)
  208. {
  209. int i = 0;
  210. if (imped < imped_index[i].imped_val) {
  211. pr_debug("%s, detected impedance is less than 4 Ohm\n",
  212. __func__);
  213. i = 0;
  214. goto ret;
  215. }
  216. if (imped >= imped_index[ARRAY_SIZE(imped_index) - 1].imped_val) {
  217. pr_debug("%s, detected impedance is greater than 12 Ohm\n",
  218. __func__);
  219. i = ARRAY_SIZE(imped_index) - 1;
  220. goto ret;
  221. }
  222. for (i = 0; i < ARRAY_SIZE(imped_index) - 1; i++) {
  223. if (imped >= imped_index[i].imped_val &&
  224. imped < imped_index[i + 1].imped_val)
  225. break;
  226. }
  227. ret:
  228. pr_debug("%s: selected impedance index = %d\n",
  229. __func__, imped_index[i].index);
  230. return imped_index[i].index;
  231. }
  232. /*
  233. * Function: wcd_clsh_imped_config
  234. * Params: component, imped, reset
  235. * Description:
  236. * This function updates HPHL and HPHR gain settings
  237. * according to the impedance value.
  238. */
  239. void wcd_clsh_imped_config(struct snd_soc_component *component, int imped,
  240. bool reset)
  241. {
  242. int i;
  243. int index = 0;
  244. int table_size;
  245. static const struct wcd_reg_mask_val
  246. (*imped_table_ptr)[MAX_IMPED_PARAMS];
  247. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  248. if (IS_CODEC_TYPE(wcd9xxx, WCD934X)) {
  249. table_size = ARRAY_SIZE(imped_table_tavil);
  250. imped_table_ptr = imped_table_tavil;
  251. } else {
  252. table_size = ARRAY_SIZE(imped_table);
  253. imped_table_ptr = imped_table;
  254. }
  255. /* reset = 1, which means request is to reset the register values */
  256. if (reset) {
  257. for (i = 0; i < MAX_IMPED_PARAMS; i++)
  258. snd_soc_component_update_bits(component,
  259. imped_table_ptr[index][i].reg,
  260. imped_table_ptr[index][i].mask, 0);
  261. return;
  262. }
  263. index = get_impedance_index(imped);
  264. if (index >= (ARRAY_SIZE(imped_index) - 1)) {
  265. pr_debug("%s, impedance not in range = %d\n", __func__, imped);
  266. return;
  267. }
  268. if (index >= table_size) {
  269. pr_debug("%s, impedance index not in range = %d\n", __func__,
  270. index);
  271. return;
  272. }
  273. for (i = 0; i < MAX_IMPED_PARAMS; i++)
  274. snd_soc_component_update_bits(component,
  275. imped_table_ptr[index][i].reg,
  276. imped_table_ptr[index][i].mask,
  277. imped_table_ptr[index][i].val);
  278. }
  279. EXPORT_SYMBOL(wcd_clsh_imped_config);
  280. static bool is_native_44_1_active(struct snd_soc_component *component)
  281. {
  282. bool native_active = false;
  283. u8 native_clk, rx1_rate, rx2_rate;
  284. native_clk = snd_soc_component_read32(component,
  285. WCD9XXX_CDC_CLK_RST_CTRL_MCLK_CONTROL);
  286. rx1_rate = snd_soc_component_read32(component,
  287. WCD9XXX_CDC_RX1_RX_PATH_CTL);
  288. rx2_rate = snd_soc_component_read32(component,
  289. WCD9XXX_CDC_RX2_RX_PATH_CTL);
  290. dev_dbg(component->dev, "%s: native_clk %x rx1_rate= %x rx2_rate= %x",
  291. __func__, native_clk, rx1_rate, rx2_rate);
  292. if ((native_clk & 0x2) &&
  293. ((rx1_rate & 0x0F) == 0x9 || (rx2_rate & 0x0F) == 0x9))
  294. native_active = true;
  295. return native_active;
  296. }
  297. static const char *mode_to_str(int mode)
  298. {
  299. switch (mode) {
  300. case CLS_H_NORMAL:
  301. return "CLS_H_NORMAL";
  302. case CLS_H_HIFI:
  303. return "CLS_H_HIFI";
  304. case CLS_H_LOHIFI:
  305. return "CLS_H_LOHIFI";
  306. case CLS_H_LP:
  307. return "CLS_H_LP";
  308. case CLS_H_ULP:
  309. return "CLS_H_ULP";
  310. case CLS_AB:
  311. return "CLS_AB";
  312. case CLS_AB_HIFI:
  313. return "CLS_AB_HIFI";
  314. default:
  315. return "CLS_H_INVALID";
  316. };
  317. }
  318. static const char *state_to_str(u8 state, char *buf, size_t buflen)
  319. {
  320. int i;
  321. int cnt = 0;
  322. /*
  323. * This array of strings should match with enum wcd_clsh_state_bit.
  324. */
  325. static const char *const states[] = {
  326. "STATE_EAR",
  327. "STATE_HPH_L",
  328. "STATE_HPH_R",
  329. "STATE_LO",
  330. };
  331. if (state == WCD_CLSH_STATE_IDLE) {
  332. snprintf(buf, buflen, "[STATE_IDLE]");
  333. goto done;
  334. }
  335. buf[0] = '\0';
  336. for (i = 0; i < ARRAY_SIZE(states); i++) {
  337. if (!(state & (1 << i)))
  338. continue;
  339. cnt = snprintf(buf, buflen - cnt - 1, "%s%s%s", buf,
  340. buf[0] == '\0' ? "[" : "|",
  341. states[i]);
  342. }
  343. if (cnt > 0)
  344. strlcat(buf + cnt, "]", buflen);
  345. done:
  346. if (buf[0] == '\0')
  347. snprintf(buf, buflen, "[STATE_UNKNOWN]");
  348. return buf;
  349. }
  350. static inline void
  351. wcd_enable_clsh_block(struct snd_soc_component *component,
  352. struct wcd_clsh_cdc_data *clsh_d, bool enable)
  353. {
  354. if ((enable && ++clsh_d->clsh_users == 1) ||
  355. (!enable && --clsh_d->clsh_users == 0))
  356. snd_soc_component_update_bits(component, WCD9XXX_A_CDC_CLSH_CRC,
  357. 0x01, (u8) enable);
  358. if (clsh_d->clsh_users < 0)
  359. clsh_d->clsh_users = 0;
  360. dev_dbg(component->dev, "%s: clsh_users %d, enable %d", __func__,
  361. clsh_d->clsh_users, enable);
  362. }
  363. static inline bool wcd_clsh_enable_status(struct snd_soc_component *component)
  364. {
  365. return snd_soc_component_read32(component, WCD9XXX_A_CDC_CLSH_CRC) &
  366. 0x01;
  367. }
  368. static inline int wcd_clsh_get_int_mode(struct wcd_clsh_cdc_data *clsh_d,
  369. int clsh_state)
  370. {
  371. int mode;
  372. if ((clsh_state != WCD_CLSH_STATE_EAR) &&
  373. (clsh_state != WCD_CLSH_STATE_HPHL) &&
  374. (clsh_state != WCD_CLSH_STATE_HPHR) &&
  375. (clsh_state != WCD_CLSH_STATE_LO))
  376. mode = CLS_NONE;
  377. else
  378. mode = clsh_d->interpolator_modes[ffs(clsh_state)];
  379. return mode;
  380. }
  381. static inline void wcd_clsh_set_int_mode(struct wcd_clsh_cdc_data *clsh_d,
  382. int clsh_state, int mode)
  383. {
  384. if ((clsh_state != WCD_CLSH_STATE_EAR) &&
  385. (clsh_state != WCD_CLSH_STATE_HPHL) &&
  386. (clsh_state != WCD_CLSH_STATE_HPHR) &&
  387. (clsh_state != WCD_CLSH_STATE_LO))
  388. return;
  389. clsh_d->interpolator_modes[ffs(clsh_state)] = mode;
  390. }
  391. static inline void wcd_clsh_set_buck_mode(struct snd_soc_component *component,
  392. int mode)
  393. {
  394. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  395. mode == CLS_AB_HIFI || mode == CLS_AB)
  396. snd_soc_component_update_bits(component,
  397. WCD9XXX_A_ANA_RX_SUPPLIES,
  398. 0x08, 0x08); /* set to HIFI */
  399. else
  400. snd_soc_component_update_bits(component,
  401. WCD9XXX_A_ANA_RX_SUPPLIES,
  402. 0x08, 0x00); /* set to default */
  403. }
  404. static inline void wcd_clsh_set_flyback_mode(
  405. struct snd_soc_component *component,
  406. int mode)
  407. {
  408. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  409. mode == CLS_AB_HIFI || mode == CLS_AB)
  410. snd_soc_component_update_bits(component,
  411. WCD9XXX_A_ANA_RX_SUPPLIES,
  412. 0x04, 0x04); /* set to HIFI */
  413. else
  414. snd_soc_component_update_bits(component,
  415. WCD9XXX_A_ANA_RX_SUPPLIES,
  416. 0x04, 0x00); /* set to Default */
  417. }
  418. static inline void wcd_clsh_gm3_boost_disable(
  419. struct snd_soc_component *component,
  420. int mode)
  421. {
  422. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  423. if (!IS_CODEC_TYPE(wcd9xxx, WCD934X))
  424. return;
  425. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  426. mode == CLS_AB_HIFI || mode == CLS_AB) {
  427. if (TAVIL_IS_1_0(wcd9xxx))
  428. snd_soc_component_update_bits(component,
  429. WCD9XXX_HPH_CNP_WG_CTL,
  430. 0x80, 0x0); /* disable GM3 Boost */
  431. snd_soc_component_update_bits(component,
  432. WCD9XXX_FLYBACK_VNEG_CTRL_4,
  433. 0xF0, 0x80);
  434. } else {
  435. snd_soc_component_update_bits(component,
  436. WCD9XXX_HPH_CNP_WG_CTL,
  437. 0x80, 0x80); /* set to Default */
  438. snd_soc_component_update_bits(component,
  439. WCD9XXX_FLYBACK_VNEG_CTRL_4,
  440. 0xF0, 0x70);
  441. }
  442. }
  443. static inline void wcd_clsh_force_iq_ctl(struct snd_soc_component *component,
  444. int mode)
  445. {
  446. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  447. if (!IS_CODEC_TYPE(wcd9xxx, WCD934X))
  448. return;
  449. if (mode == CLS_H_LOHIFI || mode == CLS_AB) {
  450. snd_soc_component_update_bits(component,
  451. WCD9XXX_HPH_NEW_INT_PA_MISC2,
  452. 0x20, 0x20);
  453. snd_soc_component_update_bits(component,
  454. WCD9XXX_RX_BIAS_HPH_LOWPOWER,
  455. 0xF0, 0xC0);
  456. snd_soc_component_update_bits(component,
  457. WCD9XXX_HPH_PA_CTL1,
  458. 0x0E, 0x02);
  459. } else {
  460. snd_soc_component_update_bits(component,
  461. WCD9XXX_HPH_NEW_INT_PA_MISC2,
  462. 0x20, 0x0);
  463. snd_soc_component_update_bits(component,
  464. WCD9XXX_RX_BIAS_HPH_LOWPOWER,
  465. 0xF0, 0x80);
  466. snd_soc_component_update_bits(component,
  467. WCD9XXX_HPH_PA_CTL1,
  468. 0x0E, 0x06);
  469. }
  470. }
  471. static void wcd_clsh_buck_ctrl(struct snd_soc_component *component,
  472. struct wcd_clsh_cdc_data *clsh_d,
  473. int mode,
  474. bool enable)
  475. {
  476. /* enable/disable buck */
  477. if ((enable && (++clsh_d->buck_users == 1)) ||
  478. (!enable && (--clsh_d->buck_users == 0)))
  479. snd_soc_component_update_bits(component,
  480. WCD9XXX_A_ANA_RX_SUPPLIES,
  481. (1 << 7), (enable << 7));
  482. dev_dbg(component->dev, "%s: buck_users %d, enable %d, mode: %s",
  483. __func__, clsh_d->buck_users, enable, mode_to_str(mode));
  484. /*
  485. * 500us sleep is required after buck enable/disable
  486. * as per HW requirement
  487. */
  488. usleep_range(500, 500 + WCD_USLEEP_RANGE);
  489. }
  490. static void wcd_clsh_flyback_ctrl(struct snd_soc_component *component,
  491. struct wcd_clsh_cdc_data *clsh_d,
  492. int mode,
  493. bool enable)
  494. {
  495. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  496. struct wcd9xxx_reg_val bulk_reg[2];
  497. u8 vneg[] = {0x00, 0x40};
  498. /* enable/disable flyback */
  499. if ((enable && (++clsh_d->flyback_users == 1)) ||
  500. (!enable && (--clsh_d->flyback_users == 0))) {
  501. snd_soc_component_update_bits(component,
  502. WCD9XXX_A_ANA_RX_SUPPLIES,
  503. (1 << 6), (enable << 6));
  504. /* 100usec delay is needed as per HW requirement */
  505. usleep_range(100, 110);
  506. if (enable && (TASHA_IS_1_1(wcd9xxx))) {
  507. wcd_clsh_set_flyback_mode(component, CLS_H_HIFI);
  508. snd_soc_component_update_bits(component,
  509. WCD9XXX_FLYBACK_EN,
  510. 0x60, 0x40);
  511. snd_soc_component_update_bits(component,
  512. WCD9XXX_FLYBACK_EN,
  513. 0x10, 0x10);
  514. vneg[0] = snd_soc_component_read32(component,
  515. WCD9XXX_A_ANA_RX_SUPPLIES);
  516. vneg[0] &= ~(0x40);
  517. vneg[1] = vneg[0] | 0x40;
  518. bulk_reg[0].reg = WCD9XXX_A_ANA_RX_SUPPLIES;
  519. bulk_reg[0].buf = &vneg[0];
  520. bulk_reg[0].bytes = 1;
  521. bulk_reg[1].reg = WCD9XXX_A_ANA_RX_SUPPLIES;
  522. bulk_reg[1].buf = &vneg[1];
  523. bulk_reg[1].bytes = 1;
  524. /* 500usec delay is needed as per HW requirement */
  525. usleep_range(500, 510);
  526. wcd9xxx_slim_bulk_write(wcd9xxx, bulk_reg, 2,
  527. false);
  528. snd_soc_component_update_bits(component,
  529. WCD9XXX_FLYBACK_EN,
  530. 0x10, 0x00);
  531. wcd_clsh_set_flyback_mode(component, mode);
  532. }
  533. }
  534. dev_dbg(component->dev, "%s: flyback_users %d, enable %d, mode: %s",
  535. __func__, clsh_d->flyback_users, enable, mode_to_str(mode));
  536. /*
  537. * 500us sleep is required after flyback enable/disable
  538. * as per HW requirement
  539. */
  540. usleep_range(500, 500 + WCD_USLEEP_RANGE);
  541. }
  542. static void wcd_clsh_set_gain_path(struct snd_soc_component *component,
  543. int mode)
  544. {
  545. u8 val = 0;
  546. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  547. if (!TASHA_IS_2_0(wcd9xxx))
  548. return;
  549. switch (mode) {
  550. case CLS_H_NORMAL:
  551. case CLS_AB:
  552. val = 0x00;
  553. break;
  554. case CLS_H_HIFI:
  555. val = 0x02;
  556. break;
  557. case CLS_H_LP:
  558. val = 0x01;
  559. break;
  560. default:
  561. return;
  562. };
  563. snd_soc_component_update_bits(component, WCD9XXX_HPH_L_EN,
  564. 0xC0, (val << 6));
  565. snd_soc_component_update_bits(component, WCD9XXX_HPH_R_EN,
  566. 0xC0, (val << 6));
  567. }
  568. static void wcd_clsh_set_hph_mode(struct snd_soc_component *component,
  569. int mode)
  570. {
  571. u8 val = 0;
  572. u8 gain = 0;
  573. u8 res_val = VREF_FILT_R_0OHM;
  574. u8 ipeak = DELTA_I_50MA;
  575. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  576. switch (mode) {
  577. case CLS_H_NORMAL:
  578. res_val = VREF_FILT_R_50KOHM;
  579. val = 0x00;
  580. gain = DAC_GAIN_0DB;
  581. ipeak = DELTA_I_50MA;
  582. break;
  583. case CLS_AB:
  584. val = 0x00;
  585. gain = DAC_GAIN_0DB;
  586. ipeak = DELTA_I_50MA;
  587. break;
  588. case CLS_AB_HIFI:
  589. val = 0x08;
  590. break;
  591. case CLS_H_HIFI:
  592. val = 0x08;
  593. gain = DAC_GAIN_M0P2DB;
  594. ipeak = DELTA_I_50MA;
  595. break;
  596. case CLS_H_LOHIFI:
  597. val = 0x00;
  598. if ((IS_CODEC_TYPE(wcd9xxx, WCD9335)) ||
  599. (IS_CODEC_TYPE(wcd9xxx, WCD9326))) {
  600. val = 0x08;
  601. gain = DAC_GAIN_M0P2DB;
  602. ipeak = DELTA_I_50MA;
  603. }
  604. break;
  605. case CLS_H_ULP:
  606. val = 0x0C;
  607. break;
  608. case CLS_H_LP:
  609. val = 0x04;
  610. ipeak = DELTA_I_30MA;
  611. break;
  612. default:
  613. return;
  614. };
  615. /*
  616. * For tavil set mode to Lower_power for
  617. * CLS_H_LOHIFI and CLS_AB
  618. */
  619. if ((IS_CODEC_TYPE(wcd9xxx, WCD934X)) &&
  620. (mode == CLS_H_LOHIFI || mode == CLS_AB))
  621. val = 0x04;
  622. snd_soc_component_update_bits(component, WCD9XXX_A_ANA_HPH, 0x0C, val);
  623. if (TASHA_IS_2_0(wcd9xxx)) {
  624. snd_soc_component_update_bits(component,
  625. WCD9XXX_CLASSH_CTRL_VCL_2,
  626. 0x30, (res_val << 4));
  627. if (mode != CLS_H_LP)
  628. snd_soc_component_update_bits(component,
  629. WCD9XXX_HPH_REFBUFF_UHQA_CTL,
  630. 0x07, gain);
  631. snd_soc_component_update_bits(component,
  632. WCD9XXX_CLASSH_CTRL_CCL_1,
  633. 0xF0, (ipeak << 4));
  634. }
  635. }
  636. static void wcd_clsh_set_flyback_vneg_ctl(struct snd_soc_component *component,
  637. bool enable)
  638. {
  639. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  640. if (!TASHA_IS_2_0(wcd9xxx))
  641. return;
  642. if (enable) {
  643. snd_soc_component_update_bits(component,
  644. WCD9XXX_FLYBACK_VNEG_CTRL_1, 0xE0, 0x00);
  645. snd_soc_component_update_bits(component,
  646. WCD9XXX_FLYBACK_VNEGDAC_CTRL_2,
  647. 0xE0, (0x07 << 5));
  648. } else {
  649. snd_soc_component_update_bits(component,
  650. WCD9XXX_FLYBACK_VNEG_CTRL_1,
  651. 0xE0, (0x07 << 5));
  652. snd_soc_component_update_bits(component,
  653. WCD9XXX_FLYBACK_VNEGDAC_CTRL_2,
  654. 0xE0, (0x02 << 5));
  655. }
  656. }
  657. static void wcd_clsh_set_flyback_current(struct snd_soc_component *component,
  658. int mode)
  659. {
  660. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  661. if (!TASHA_IS_2_0(wcd9xxx))
  662. return;
  663. snd_soc_component_update_bits(component, WCD9XXX_RX_BIAS_FLYB_BUFF,
  664. 0x0F, 0x0A);
  665. snd_soc_component_update_bits(component, WCD9XXX_RX_BIAS_FLYB_BUFF,
  666. 0xF0, 0xA0);
  667. /* Sleep needed to avoid click and pop as per HW requirement */
  668. usleep_range(100, 110);
  669. }
  670. static void wcd_clsh_set_buck_regulator_mode(
  671. struct snd_soc_component *component,
  672. int mode)
  673. {
  674. snd_soc_component_update_bits(component, WCD9XXX_A_ANA_RX_SUPPLIES,
  675. 0x02, 0x00);
  676. }
  677. static void wcd_clsh_state_lo(struct snd_soc_component *component,
  678. struct wcd_clsh_cdc_data *clsh_d,
  679. u8 req_state, bool is_enable, int mode)
  680. {
  681. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  682. mode_to_str(mode),
  683. is_enable ? "enable" : "disable");
  684. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  685. dev_err_ratelimited(component->dev, "%s: LO cannot be in this mode: %d\n",
  686. __func__, mode);
  687. return;
  688. }
  689. if (is_enable) {
  690. wcd_clsh_set_buck_regulator_mode(component, mode);
  691. wcd_clsh_set_flyback_vneg_ctl(component, true);
  692. wcd_clsh_set_buck_mode(component, mode);
  693. wcd_clsh_set_flyback_mode(component, mode);
  694. wcd_clsh_flyback_ctrl(component, clsh_d, mode, true);
  695. wcd_clsh_set_flyback_current(component, mode);
  696. wcd_clsh_buck_ctrl(component, clsh_d, mode, true);
  697. } else {
  698. wcd_clsh_buck_ctrl(component, clsh_d, mode, false);
  699. wcd_clsh_flyback_ctrl(component, clsh_d, mode, false);
  700. wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL);
  701. wcd_clsh_set_buck_mode(component, CLS_H_NORMAL);
  702. wcd_clsh_set_flyback_vneg_ctl(component, false);
  703. wcd_clsh_set_buck_regulator_mode(component, CLS_H_NORMAL);
  704. }
  705. }
  706. static void wcd_clsh_state_hph_ear(struct snd_soc_component *component,
  707. struct wcd_clsh_cdc_data *clsh_d,
  708. u8 req_state, bool is_enable, int mode)
  709. {
  710. int hph_mode = 0;
  711. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  712. mode_to_str(mode),
  713. is_enable ? "enable" : "disable");
  714. if (is_enable) {
  715. if (req_state == WCD_CLSH_STATE_EAR) {
  716. /* If HPH is running in CLS-AB when
  717. * EAR comes, let it continue to run
  718. * in Class-AB, no need to enable Class-H
  719. * for EAR.
  720. */
  721. if (clsh_d->state & WCD_CLSH_STATE_HPHL)
  722. hph_mode = wcd_clsh_get_int_mode(clsh_d,
  723. WCD_CLSH_STATE_HPHL);
  724. else if (clsh_d->state & WCD_CLSH_STATE_HPHR)
  725. hph_mode = wcd_clsh_get_int_mode(clsh_d,
  726. WCD_CLSH_STATE_HPHR);
  727. else
  728. return;
  729. if (hph_mode != CLS_AB && hph_mode != CLS_AB_HIFI
  730. && !is_native_44_1_active(component))
  731. snd_soc_component_update_bits(component,
  732. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  733. 0x40, 0x40);
  734. }
  735. if (is_native_44_1_active(component)) {
  736. snd_soc_component_write(component,
  737. WCD9XXX_CDC_CLSH_HPH_V_PA, 0x39);
  738. snd_soc_component_update_bits(component,
  739. WCD9XXX_CDC_RX0_RX_PATH_SEC0,
  740. 0x03, 0x00);
  741. if ((req_state == WCD_CLSH_STATE_HPHL) ||
  742. (req_state == WCD_CLSH_STATE_HPHR))
  743. snd_soc_component_update_bits(component,
  744. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  745. 0x40, 0x00);
  746. }
  747. if (req_state == WCD_CLSH_STATE_HPHL)
  748. snd_soc_component_update_bits(component,
  749. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  750. 0x40, 0x40);
  751. if (req_state == WCD_CLSH_STATE_HPHR)
  752. snd_soc_component_update_bits(component,
  753. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  754. 0x40, 0x40);
  755. if ((req_state == WCD_CLSH_STATE_HPHL) ||
  756. (req_state == WCD_CLSH_STATE_HPHR)) {
  757. wcd_clsh_set_gain_path(component, mode);
  758. wcd_clsh_set_flyback_mode(component, mode);
  759. wcd_clsh_set_buck_mode(component, mode);
  760. }
  761. } else {
  762. if (req_state == WCD_CLSH_STATE_EAR) {
  763. /*
  764. * If EAR goes away, disable EAR Channel Enable
  765. * if HPH running in Class-H otherwise
  766. * and if HPH requested mode is CLS_AB then
  767. * no need to disable EAR channel enable bit.
  768. */
  769. if (wcd_clsh_enable_status(component))
  770. snd_soc_component_update_bits(component,
  771. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  772. 0x40, 0x00);
  773. }
  774. if (is_native_44_1_active(component)) {
  775. snd_soc_component_write(component,
  776. WCD9XXX_CDC_CLSH_HPH_V_PA, 0x1C);
  777. snd_soc_component_update_bits(component,
  778. WCD9XXX_CDC_RX0_RX_PATH_SEC0,
  779. 0x03, 0x01);
  780. if (((clsh_d->state & WCD_CLSH_STATE_HPH_ST)
  781. != WCD_CLSH_STATE_HPH_ST) &&
  782. ((req_state == WCD_CLSH_STATE_HPHL) ||
  783. (req_state == WCD_CLSH_STATE_HPHR)))
  784. snd_soc_component_update_bits(component,
  785. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  786. 0x40, 0x40);
  787. }
  788. if (req_state == WCD_CLSH_STATE_HPHL)
  789. snd_soc_component_update_bits(component,
  790. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  791. 0x40, 0x00);
  792. if (req_state == WCD_CLSH_STATE_HPHR)
  793. snd_soc_component_update_bits(component,
  794. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  795. 0x40, 0x00);
  796. if ((req_state & WCD_CLSH_STATE_HPH_ST) &&
  797. !wcd_clsh_enable_status(component)) {
  798. /* If Class-H is not enabled when HPH is turned
  799. * off, enable it as EAR is in progress
  800. */
  801. wcd_enable_clsh_block(component, clsh_d, true);
  802. snd_soc_component_update_bits(component,
  803. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  804. 0x40, 0x40);
  805. wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL);
  806. wcd_clsh_set_buck_mode(component, CLS_H_NORMAL);
  807. }
  808. }
  809. }
  810. static void wcd_clsh_state_ear_lo(struct snd_soc_component *component,
  811. struct wcd_clsh_cdc_data *clsh_d,
  812. u8 req_state, bool is_enable, int mode)
  813. {
  814. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  815. mode_to_str(mode),
  816. is_enable ? "enable" : "disable");
  817. if (is_enable) {
  818. /* LO powerup is taken care in PA sequence.
  819. * No need to change to class AB here.
  820. */
  821. if (req_state == WCD_CLSH_STATE_EAR) {
  822. /* EAR powerup.*/
  823. if (!wcd_clsh_enable_status(component)) {
  824. wcd_enable_clsh_block(component, clsh_d, true);
  825. wcd_clsh_set_buck_mode(component, mode);
  826. wcd_clsh_set_flyback_mode(component, mode);
  827. }
  828. snd_soc_component_update_bits(component,
  829. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  830. 0x40, 0x40);
  831. }
  832. } else {
  833. if (req_state == WCD_CLSH_STATE_EAR) {
  834. /* EAR powerdown.*/
  835. wcd_enable_clsh_block(component, clsh_d, false);
  836. wcd_clsh_set_buck_mode(component, CLS_H_NORMAL);
  837. wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL);
  838. snd_soc_component_update_bits(component,
  839. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  840. 0x40, 0x00);
  841. }
  842. /* LO powerdown is taken care in PA sequence.
  843. * No need to change to class H here.
  844. */
  845. }
  846. }
  847. static void wcd_clsh_state_hph_lo(struct snd_soc_component *component,
  848. struct wcd_clsh_cdc_data *clsh_d,
  849. u8 req_state, bool is_enable, int mode)
  850. {
  851. int hph_mode = 0;
  852. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  853. mode_to_str(mode),
  854. is_enable ? "enable" : "disable");
  855. if (is_enable) {
  856. /*
  857. * If requested state is LO, put regulator
  858. * in class-AB or if requested state is HPH,
  859. * which means LO is already enabled, keep
  860. * the regulator config the same at class-AB
  861. * and just set the power modes for flyback
  862. * and buck.
  863. */
  864. if (req_state == WCD_CLSH_STATE_LO)
  865. wcd_clsh_set_buck_regulator_mode(component, CLS_AB);
  866. else {
  867. if (!wcd_clsh_enable_status(component)) {
  868. wcd_enable_clsh_block(component, clsh_d, true);
  869. snd_soc_component_update_bits(component,
  870. WCD9XXX_A_CDC_CLSH_K1_MSB,
  871. 0x0F, 0x00);
  872. snd_soc_component_update_bits(component,
  873. WCD9XXX_A_CDC_CLSH_K1_LSB,
  874. 0xFF, 0xC0);
  875. wcd_clsh_set_flyback_mode(component, mode);
  876. wcd_clsh_set_flyback_vneg_ctl(component, false);
  877. wcd_clsh_set_buck_mode(component, mode);
  878. wcd_clsh_set_hph_mode(component, mode);
  879. wcd_clsh_set_gain_path(component, mode);
  880. } else {
  881. dev_dbg(component->dev, "%s:clsh is already enabled\n",
  882. __func__);
  883. }
  884. if (req_state == WCD_CLSH_STATE_HPHL)
  885. snd_soc_component_update_bits(component,
  886. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  887. 0x40, 0x40);
  888. if (req_state == WCD_CLSH_STATE_HPHR)
  889. snd_soc_component_update_bits(component,
  890. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  891. 0x40, 0x40);
  892. }
  893. } else {
  894. if ((req_state == WCD_CLSH_STATE_HPHL) ||
  895. (req_state == WCD_CLSH_STATE_HPHR)) {
  896. if (req_state == WCD_CLSH_STATE_HPHL)
  897. snd_soc_component_update_bits(component,
  898. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  899. 0x40, 0x00);
  900. if (req_state == WCD_CLSH_STATE_HPHR)
  901. snd_soc_component_update_bits(component,
  902. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  903. 0x40, 0x00);
  904. /*
  905. * If HPH is powering down first, then disable clsh,
  906. * set the buck/flyback mode to default and keep the
  907. * regulator at Class-AB
  908. */
  909. if ((clsh_d->state & WCD_CLSH_STATE_HPH_ST)
  910. != WCD_CLSH_STATE_HPH_ST) {
  911. wcd_enable_clsh_block(component, clsh_d, false);
  912. wcd_clsh_set_flyback_vneg_ctl(component, true);
  913. wcd_clsh_set_flyback_mode(
  914. component, CLS_H_NORMAL);
  915. wcd_clsh_set_buck_mode(component, CLS_H_NORMAL);
  916. }
  917. } else {
  918. /* LO powerdown.
  919. * If HPH mode also is CLS-AB, no need
  920. * to turn-on class-H, otherwise enable
  921. * Class-H configuration.
  922. */
  923. if (clsh_d->state & WCD_CLSH_STATE_HPHL)
  924. hph_mode = wcd_clsh_get_int_mode(clsh_d,
  925. WCD_CLSH_STATE_HPHL);
  926. else if (clsh_d->state & WCD_CLSH_STATE_HPHR)
  927. hph_mode = wcd_clsh_get_int_mode(clsh_d,
  928. WCD_CLSH_STATE_HPHR);
  929. else
  930. return;
  931. dev_dbg(component->dev, "%s: hph_mode = %d\n", __func__,
  932. hph_mode);
  933. if ((hph_mode == CLS_AB) ||
  934. (hph_mode == CLS_AB_HIFI) ||
  935. (hph_mode == CLS_NONE))
  936. goto end;
  937. /*
  938. * If Class-H is already enabled (HPH ON and then
  939. * LO ON), no need to turn on again, just set the
  940. * regulator mode.
  941. */
  942. if (wcd_clsh_enable_status(component)) {
  943. wcd_clsh_set_buck_regulator_mode(component,
  944. hph_mode);
  945. goto end;
  946. } else {
  947. dev_dbg(component->dev, "%s: clsh is not enabled\n",
  948. __func__);
  949. }
  950. wcd_enable_clsh_block(component, clsh_d, true);
  951. snd_soc_component_update_bits(component,
  952. WCD9XXX_A_CDC_CLSH_K1_MSB,
  953. 0x0F, 0x00);
  954. snd_soc_component_update_bits(component,
  955. WCD9XXX_A_CDC_CLSH_K1_LSB,
  956. 0xFF, 0xC0);
  957. wcd_clsh_set_buck_regulator_mode(component,
  958. hph_mode);
  959. if (clsh_d->state & WCD_CLSH_STATE_HPHL)
  960. snd_soc_component_update_bits(component,
  961. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  962. 0x40, 0x40);
  963. if (clsh_d->state & WCD_CLSH_STATE_HPHR)
  964. snd_soc_component_update_bits(component,
  965. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  966. 0x40, 0x40);
  967. wcd_clsh_set_hph_mode(component, hph_mode);
  968. }
  969. }
  970. end:
  971. return;
  972. }
  973. static void wcd_clsh_state_hph_st(struct snd_soc_component *component,
  974. struct wcd_clsh_cdc_data *clsh_d,
  975. u8 req_state, bool is_enable, int mode)
  976. {
  977. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  978. mode_to_str(mode),
  979. is_enable ? "enable" : "disable");
  980. if (mode == CLS_AB || mode == CLS_AB_HIFI)
  981. return;
  982. if (is_enable) {
  983. if (req_state == WCD_CLSH_STATE_HPHL)
  984. snd_soc_component_update_bits(component,
  985. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  986. 0x40, 0x40);
  987. if (req_state == WCD_CLSH_STATE_HPHR)
  988. snd_soc_component_update_bits(component,
  989. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  990. 0x40, 0x40);
  991. } else {
  992. if (req_state == WCD_CLSH_STATE_HPHL)
  993. snd_soc_component_update_bits(component,
  994. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  995. 0x40, 0x00);
  996. if (req_state == WCD_CLSH_STATE_HPHR)
  997. snd_soc_component_update_bits(component,
  998. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  999. 0x40, 0x00);
  1000. }
  1001. }
  1002. static void wcd_clsh_state_hph_r(struct snd_soc_component *component,
  1003. struct wcd_clsh_cdc_data *clsh_d,
  1004. u8 req_state, bool is_enable, int mode)
  1005. {
  1006. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  1007. mode_to_str(mode),
  1008. is_enable ? "enable" : "disable");
  1009. if (mode == CLS_H_NORMAL) {
  1010. dev_err_ratelimited(component->dev, "%s: Normal mode not applicable for hph_r\n",
  1011. __func__);
  1012. return;
  1013. }
  1014. if (is_enable) {
  1015. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  1016. wcd_enable_clsh_block(component, clsh_d, true);
  1017. /*
  1018. * These K1 values depend on the Headphone Impedance
  1019. * For now it is assumed to be 16 ohm
  1020. */
  1021. snd_soc_component_update_bits(component,
  1022. WCD9XXX_A_CDC_CLSH_K1_MSB,
  1023. 0x0F, 0x00);
  1024. snd_soc_component_update_bits(component,
  1025. WCD9XXX_A_CDC_CLSH_K1_LSB,
  1026. 0xFF, 0xC0);
  1027. snd_soc_component_update_bits(component,
  1028. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  1029. 0x40, 0x40);
  1030. }
  1031. wcd_clsh_set_buck_regulator_mode(component, mode);
  1032. wcd_clsh_set_flyback_mode(component, mode);
  1033. wcd_clsh_gm3_boost_disable(component, mode);
  1034. wcd_clsh_force_iq_ctl(component, mode);
  1035. wcd_clsh_flyback_ctrl(component, clsh_d, mode, true);
  1036. wcd_clsh_set_flyback_current(component, mode);
  1037. wcd_clsh_set_buck_mode(component, mode);
  1038. wcd_clsh_buck_ctrl(component, clsh_d, mode, true);
  1039. wcd_clsh_set_hph_mode(component, mode);
  1040. wcd_clsh_set_gain_path(component, mode);
  1041. } else {
  1042. wcd_clsh_set_hph_mode(component, CLS_H_NORMAL);
  1043. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  1044. snd_soc_component_update_bits(component,
  1045. WCD9XXX_A_CDC_RX2_RX_PATH_CFG0,
  1046. 0x40, 0x00);
  1047. wcd_enable_clsh_block(component, clsh_d, false);
  1048. }
  1049. /* buck and flyback set to default mode and disable */
  1050. wcd_clsh_buck_ctrl(component, clsh_d, CLS_H_NORMAL, false);
  1051. wcd_clsh_flyback_ctrl(component, clsh_d, CLS_H_NORMAL, false);
  1052. wcd_clsh_force_iq_ctl(component, CLS_H_NORMAL);
  1053. wcd_clsh_gm3_boost_disable(component, CLS_H_NORMAL);
  1054. wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL);
  1055. wcd_clsh_set_buck_mode(component, CLS_H_NORMAL);
  1056. wcd_clsh_set_buck_regulator_mode(component, CLS_H_NORMAL);
  1057. }
  1058. }
  1059. static void wcd_clsh_state_hph_l(struct snd_soc_component *component,
  1060. struct wcd_clsh_cdc_data *clsh_d,
  1061. u8 req_state, bool is_enable, int mode)
  1062. {
  1063. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  1064. mode_to_str(mode),
  1065. is_enable ? "enable" : "disable");
  1066. if (mode == CLS_H_NORMAL) {
  1067. dev_err_ratelimited(component->dev, "%s: Normal mode not applicable for hph_l\n",
  1068. __func__);
  1069. return;
  1070. }
  1071. if (is_enable) {
  1072. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  1073. wcd_enable_clsh_block(component, clsh_d, true);
  1074. /*
  1075. * These K1 values depend on the Headphone Impedance
  1076. * For now it is assumed to be 16 ohm
  1077. */
  1078. snd_soc_component_update_bits(component,
  1079. WCD9XXX_A_CDC_CLSH_K1_MSB,
  1080. 0x0F, 0x00);
  1081. snd_soc_component_update_bits(component,
  1082. WCD9XXX_A_CDC_CLSH_K1_LSB,
  1083. 0xFF, 0xC0);
  1084. snd_soc_component_update_bits(component,
  1085. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  1086. 0x40, 0x40);
  1087. }
  1088. wcd_clsh_set_buck_regulator_mode(component, mode);
  1089. wcd_clsh_set_flyback_mode(component, mode);
  1090. wcd_clsh_gm3_boost_disable(component, mode);
  1091. wcd_clsh_force_iq_ctl(component, mode);
  1092. wcd_clsh_flyback_ctrl(component, clsh_d, mode, true);
  1093. wcd_clsh_set_flyback_current(component, mode);
  1094. wcd_clsh_set_buck_mode(component, mode);
  1095. wcd_clsh_buck_ctrl(component, clsh_d, mode, true);
  1096. wcd_clsh_set_hph_mode(component, mode);
  1097. wcd_clsh_set_gain_path(component, mode);
  1098. } else {
  1099. wcd_clsh_set_hph_mode(component, CLS_H_NORMAL);
  1100. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  1101. snd_soc_component_update_bits(component,
  1102. WCD9XXX_A_CDC_RX1_RX_PATH_CFG0,
  1103. 0x40, 0x00);
  1104. wcd_enable_clsh_block(component, clsh_d, false);
  1105. }
  1106. /* set buck and flyback to Default Mode */
  1107. wcd_clsh_buck_ctrl(component, clsh_d, CLS_H_NORMAL, false);
  1108. wcd_clsh_flyback_ctrl(component, clsh_d, CLS_H_NORMAL, false);
  1109. wcd_clsh_force_iq_ctl(component, CLS_H_NORMAL);
  1110. wcd_clsh_gm3_boost_disable(component, CLS_H_NORMAL);
  1111. wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL);
  1112. wcd_clsh_set_buck_mode(component, CLS_H_NORMAL);
  1113. wcd_clsh_set_buck_regulator_mode(component, CLS_H_NORMAL);
  1114. }
  1115. }
  1116. static void wcd_clsh_state_ear(struct snd_soc_component *component,
  1117. struct wcd_clsh_cdc_data *clsh_d,
  1118. u8 req_state, bool is_enable, int mode)
  1119. {
  1120. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  1121. mode_to_str(mode),
  1122. is_enable ? "enable" : "disable");
  1123. if (mode != CLS_H_NORMAL) {
  1124. dev_err_ratelimited(component->dev, "%s: mode: %s cannot be used for EAR\n",
  1125. __func__, mode_to_str(mode));
  1126. return;
  1127. }
  1128. if (is_enable) {
  1129. wcd_enable_clsh_block(component, clsh_d, true);
  1130. snd_soc_component_update_bits(component,
  1131. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  1132. 0x40, 0x40);
  1133. wcd_clsh_set_buck_mode(component, mode);
  1134. wcd_clsh_set_flyback_mode(component, mode);
  1135. wcd_clsh_flyback_ctrl(component, clsh_d, mode, true);
  1136. wcd_clsh_set_flyback_current(component, mode);
  1137. wcd_clsh_buck_ctrl(component, clsh_d, mode, true);
  1138. } else {
  1139. snd_soc_component_update_bits(component,
  1140. WCD9XXX_A_CDC_RX0_RX_PATH_CFG0,
  1141. 0x40, 0x00);
  1142. wcd_enable_clsh_block(component, clsh_d, false);
  1143. wcd_clsh_buck_ctrl(component, clsh_d, mode, false);
  1144. wcd_clsh_flyback_ctrl(component, clsh_d, mode, false);
  1145. wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL);
  1146. wcd_clsh_set_buck_mode(component, CLS_H_NORMAL);
  1147. }
  1148. }
  1149. static void wcd_clsh_state_err(struct snd_soc_component *component,
  1150. struct wcd_clsh_cdc_data *clsh_d,
  1151. u8 req_state, bool is_enable, int mode)
  1152. {
  1153. char msg[128];
  1154. dev_err_ratelimited(component->dev,
  1155. "%s Wrong request for class H state machine requested to %s %s",
  1156. __func__, is_enable ? "enable" : "disable",
  1157. state_to_str(req_state, msg, sizeof(msg)));
  1158. WARN_ON(1);
  1159. }
  1160. /*
  1161. * Function: wcd_clsh_is_state_valid
  1162. * Params: state
  1163. * Description:
  1164. * Provides information on valid states of Class H configuration
  1165. */
  1166. static bool wcd_clsh_is_state_valid(u8 state)
  1167. {
  1168. switch (state) {
  1169. case WCD_CLSH_STATE_IDLE:
  1170. case WCD_CLSH_STATE_EAR:
  1171. case WCD_CLSH_STATE_HPHL:
  1172. case WCD_CLSH_STATE_HPHR:
  1173. case WCD_CLSH_STATE_HPH_ST:
  1174. case WCD_CLSH_STATE_LO:
  1175. case WCD_CLSH_STATE_HPHL_EAR:
  1176. case WCD_CLSH_STATE_HPHR_EAR:
  1177. case WCD_CLSH_STATE_HPH_ST_EAR:
  1178. case WCD_CLSH_STATE_HPHL_LO:
  1179. case WCD_CLSH_STATE_HPHR_LO:
  1180. case WCD_CLSH_STATE_HPH_ST_LO:
  1181. case WCD_CLSH_STATE_EAR_LO:
  1182. return true;
  1183. default:
  1184. return false;
  1185. };
  1186. }
  1187. /*
  1188. * Function: wcd_clsh_fsm
  1189. * Params: component, cdc_clsh_d, req_state, req_type, clsh_event
  1190. * Description:
  1191. * This function handles PRE DAC and POST DAC conditions of different devices
  1192. * and updates class H configuration of different combination of devices
  1193. * based on validity of their states. cdc_clsh_d will contain current
  1194. * class h state information
  1195. */
  1196. void wcd_clsh_fsm(struct snd_soc_component *component,
  1197. struct wcd_clsh_cdc_data *cdc_clsh_d,
  1198. u8 clsh_event, u8 req_state,
  1199. int int_mode)
  1200. {
  1201. u8 old_state, new_state;
  1202. char msg0[128], msg1[128];
  1203. switch (clsh_event) {
  1204. case WCD_CLSH_EVENT_PRE_DAC:
  1205. old_state = cdc_clsh_d->state;
  1206. new_state = old_state | req_state;
  1207. if (!wcd_clsh_is_state_valid(new_state)) {
  1208. dev_err_ratelimited(component->dev,
  1209. "%s: Class-H not a valid new state: %s\n",
  1210. __func__,
  1211. state_to_str(new_state, msg0, sizeof(msg0)));
  1212. return;
  1213. }
  1214. if (new_state == old_state) {
  1215. dev_err_ratelimited(component->dev,
  1216. "%s: Class-H already in requested state: %s\n",
  1217. __func__,
  1218. state_to_str(new_state, msg0, sizeof(msg0)));
  1219. return;
  1220. }
  1221. cdc_clsh_d->state = new_state;
  1222. wcd_clsh_set_int_mode(cdc_clsh_d, req_state, int_mode);
  1223. (*clsh_state_fp[new_state]) (component, cdc_clsh_d, req_state,
  1224. CLSH_REQ_ENABLE, int_mode);
  1225. dev_dbg(component->dev,
  1226. "%s: ClassH state transition from %s to %s\n",
  1227. __func__, state_to_str(old_state, msg0, sizeof(msg0)),
  1228. state_to_str(cdc_clsh_d->state, msg1, sizeof(msg1)));
  1229. break;
  1230. case WCD_CLSH_EVENT_POST_PA:
  1231. old_state = cdc_clsh_d->state;
  1232. new_state = old_state & (~req_state);
  1233. if (new_state < NUM_CLSH_STATES_V2) {
  1234. if (!wcd_clsh_is_state_valid(old_state)) {
  1235. dev_err_ratelimited(component->dev,
  1236. "%s:Invalid old state:%s\n",
  1237. __func__,
  1238. state_to_str(old_state, msg0,
  1239. sizeof(msg0)));
  1240. return;
  1241. }
  1242. if (new_state == old_state) {
  1243. dev_err_ratelimited(component->dev,
  1244. "%s: Class-H already in requested state: %s\n",
  1245. __func__,
  1246. state_to_str(new_state, msg0,
  1247. sizeof(msg0)));
  1248. return;
  1249. }
  1250. (*clsh_state_fp[old_state]) (component, cdc_clsh_d,
  1251. req_state, CLSH_REQ_DISABLE,
  1252. int_mode);
  1253. cdc_clsh_d->state = new_state;
  1254. wcd_clsh_set_int_mode(cdc_clsh_d, req_state, CLS_NONE);
  1255. dev_dbg(component->dev, "%s: ClassH state transition from %s to %s\n",
  1256. __func__, state_to_str(old_state, msg0,
  1257. sizeof(msg0)),
  1258. state_to_str(cdc_clsh_d->state, msg1,
  1259. sizeof(msg1)));
  1260. }
  1261. break;
  1262. };
  1263. }
  1264. EXPORT_SYMBOL(wcd_clsh_fsm);
  1265. int wcd_clsh_get_clsh_state(struct wcd_clsh_cdc_data *clsh)
  1266. {
  1267. return clsh->state;
  1268. }
  1269. EXPORT_SYMBOL(wcd_clsh_get_clsh_state);
  1270. void wcd_clsh_init(struct wcd_clsh_cdc_data *clsh)
  1271. {
  1272. int i;
  1273. clsh->state = WCD_CLSH_STATE_IDLE;
  1274. for (i = 0; i < NUM_CLSH_STATES_V2; i++)
  1275. clsh_state_fp[i] = wcd_clsh_state_err;
  1276. clsh_state_fp[WCD_CLSH_STATE_EAR] = wcd_clsh_state_ear;
  1277. clsh_state_fp[WCD_CLSH_STATE_HPHL] =
  1278. wcd_clsh_state_hph_l;
  1279. clsh_state_fp[WCD_CLSH_STATE_HPHR] =
  1280. wcd_clsh_state_hph_r;
  1281. clsh_state_fp[WCD_CLSH_STATE_HPH_ST] =
  1282. wcd_clsh_state_hph_st;
  1283. clsh_state_fp[WCD_CLSH_STATE_LO] = wcd_clsh_state_lo;
  1284. clsh_state_fp[WCD_CLSH_STATE_HPHL_EAR] =
  1285. wcd_clsh_state_hph_ear;
  1286. clsh_state_fp[WCD_CLSH_STATE_HPHR_EAR] =
  1287. wcd_clsh_state_hph_ear;
  1288. clsh_state_fp[WCD_CLSH_STATE_HPH_ST_EAR] =
  1289. wcd_clsh_state_hph_ear;
  1290. clsh_state_fp[WCD_CLSH_STATE_HPHL_LO] = wcd_clsh_state_hph_lo;
  1291. clsh_state_fp[WCD_CLSH_STATE_HPHR_LO] = wcd_clsh_state_hph_lo;
  1292. clsh_state_fp[WCD_CLSH_STATE_HPH_ST_LO] =
  1293. wcd_clsh_state_hph_lo;
  1294. clsh_state_fp[WCD_CLSH_STATE_EAR_LO] = wcd_clsh_state_ear_lo;
  1295. /* Set interpolaotr modes to NONE */
  1296. wcd_clsh_set_int_mode(clsh, WCD_CLSH_STATE_EAR, CLS_NONE);
  1297. wcd_clsh_set_int_mode(clsh, WCD_CLSH_STATE_HPHL, CLS_NONE);
  1298. wcd_clsh_set_int_mode(clsh, WCD_CLSH_STATE_HPHR, CLS_NONE);
  1299. wcd_clsh_set_int_mode(clsh, WCD_CLSH_STATE_LO, CLS_NONE);
  1300. clsh->flyback_users = 0;
  1301. clsh->buck_users = 0;
  1302. clsh->clsh_users = 0;
  1303. }
  1304. EXPORT_SYMBOL(wcd_clsh_init);
  1305. MODULE_DESCRIPTION("WCD9XXX Common Driver");
  1306. MODULE_LICENSE("GPL v2");