aqt1000-clsh.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/slab.h>
  6. #include <linux/kernel.h>
  7. #include <linux/delay.h>
  8. #include <sound/soc.h>
  9. #include "aqt1000-registers.h"
  10. #include "aqt1000-clsh.h"
  11. #define AQT_USLEEP_RANGE 50
  12. #define MAX_IMPED_PARAMS 6
  13. enum aqt_vref_dac_sel {
  14. VREF_N1P9V = 0,
  15. VREF_N1P86V,
  16. VREF_N181V,
  17. VREF_N1P74V,
  18. VREF_N1P7V,
  19. VREF_N0P9V,
  20. VREF_N1P576V,
  21. VREF_N1P827V,
  22. };
  23. enum aqt_vref_ctl {
  24. CONTROLLER = 0,
  25. I2C,
  26. };
  27. enum aqt_hd2_res_div_ctl {
  28. DISCONNECT = 0,
  29. P5_0P35,
  30. P75_0P68,
  31. P82_0P77,
  32. P9_0P87,
  33. };
  34. enum aqt_curr_bias_err_amp {
  35. I_0P25UA = 0,
  36. I_0P5UA,
  37. I_0P75UA,
  38. I_1UA,
  39. I_1P25UA,
  40. I_1P5UA,
  41. I_1P75UA,
  42. I_2UA,
  43. };
  44. static const struct aqt_reg_mask_val imped_table_aqt[][MAX_IMPED_PARAMS] = {
  45. {
  46. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xf2},
  47. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf2},
  48. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  49. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xf2},
  50. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf2},
  51. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  52. },
  53. {
  54. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xf4},
  55. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf4},
  56. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  57. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xf4},
  58. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf4},
  59. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  60. },
  61. {
  62. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xf7},
  63. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf7},
  64. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  65. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xf7},
  66. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf7},
  67. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  68. },
  69. {
  70. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xf9},
  71. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xf9},
  72. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  73. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xf9},
  74. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xf9},
  75. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  76. },
  77. {
  78. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xfa},
  79. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfa},
  80. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  81. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xfa},
  82. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfa},
  83. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  84. },
  85. {
  86. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xfb},
  87. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfb},
  88. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  89. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xfb},
  90. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfb},
  91. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  92. },
  93. {
  94. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xfc},
  95. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfc},
  96. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  97. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xfc},
  98. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfc},
  99. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  100. },
  101. {
  102. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xfd},
  103. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfd},
  104. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x00},
  105. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xfd},
  106. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfd},
  107. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x00},
  108. },
  109. {
  110. {AQT1000_CDC_RX1_RX_VOL_CTL, 0xff, 0xfd},
  111. {AQT1000_CDC_RX1_RX_VOL_MIX_CTL, 0xff, 0xfd},
  112. {AQT1000_CDC_RX1_RX_PATH_SEC1, 0x01, 0x01},
  113. {AQT1000_CDC_RX2_RX_VOL_CTL, 0xff, 0xfd},
  114. {AQT1000_CDC_RX2_RX_VOL_MIX_CTL, 0xff, 0xfd},
  115. {AQT1000_CDC_RX2_RX_PATH_SEC1, 0x01, 0x01},
  116. },
  117. };
  118. static const struct aqt_imped_val imped_index[] = {
  119. {4, 0},
  120. {5, 1},
  121. {6, 2},
  122. {7, 3},
  123. {8, 4},
  124. {9, 5},
  125. {10, 6},
  126. {11, 7},
  127. {12, 8},
  128. {13, 9},
  129. };
  130. static void (*clsh_state_fp[NUM_CLSH_STATES])(struct snd_soc_codec *,
  131. struct aqt_clsh_cdc_data *,
  132. u8 req_state, bool en, int mode);
  133. static int get_impedance_index(int imped)
  134. {
  135. int i = 0;
  136. if (imped < imped_index[i].imped_val) {
  137. pr_debug("%s, detected impedance is less than 4 Ohm\n",
  138. __func__);
  139. i = 0;
  140. goto ret;
  141. }
  142. if (imped >= imped_index[ARRAY_SIZE(imped_index) - 1].imped_val) {
  143. pr_debug("%s, detected impedance is greater than 12 Ohm\n",
  144. __func__);
  145. i = ARRAY_SIZE(imped_index) - 1;
  146. goto ret;
  147. }
  148. for (i = 0; i < ARRAY_SIZE(imped_index) - 1; i++) {
  149. if (imped >= imped_index[i].imped_val &&
  150. imped < imped_index[i + 1].imped_val)
  151. break;
  152. }
  153. ret:
  154. pr_debug("%s: selected impedance index = %d\n",
  155. __func__, imped_index[i].index);
  156. return imped_index[i].index;
  157. }
  158. /*
  159. * Function: aqt_clsh_imped_config
  160. * Params: codec, imped, reset
  161. * Description:
  162. * This function updates HPHL and HPHR gain settings
  163. * according to the impedance value.
  164. */
  165. void aqt_clsh_imped_config(struct snd_soc_codec *codec, int imped, bool reset)
  166. {
  167. int i;
  168. int index = 0;
  169. int table_size;
  170. static const struct aqt_reg_mask_val
  171. (*imped_table_ptr)[MAX_IMPED_PARAMS];
  172. table_size = ARRAY_SIZE(imped_table_aqt);
  173. imped_table_ptr = imped_table_aqt;
  174. /* reset = 1, which means request is to reset the register values */
  175. if (reset) {
  176. for (i = 0; i < MAX_IMPED_PARAMS; i++)
  177. snd_soc_update_bits(codec,
  178. imped_table_ptr[index][i].reg,
  179. imped_table_ptr[index][i].mask, 0);
  180. return;
  181. }
  182. index = get_impedance_index(imped);
  183. if (index >= (ARRAY_SIZE(imped_index) - 1)) {
  184. pr_debug("%s, impedance not in range = %d\n", __func__, imped);
  185. return;
  186. }
  187. if (index >= table_size) {
  188. pr_debug("%s, impedance index not in range = %d\n", __func__,
  189. index);
  190. return;
  191. }
  192. for (i = 0; i < MAX_IMPED_PARAMS; i++)
  193. snd_soc_update_bits(codec,
  194. imped_table_ptr[index][i].reg,
  195. imped_table_ptr[index][i].mask,
  196. imped_table_ptr[index][i].val);
  197. }
  198. EXPORT_SYMBOL(aqt_clsh_imped_config);
  199. static const char *mode_to_str(int mode)
  200. {
  201. switch (mode) {
  202. case CLS_H_NORMAL:
  203. return "CLS_H_NORMAL";
  204. case CLS_H_HIFI:
  205. return "CLS_H_HIFI";
  206. case CLS_H_LOHIFI:
  207. return "CLS_H_LOHIFI";
  208. case CLS_H_LP:
  209. return "CLS_H_LP";
  210. case CLS_H_ULP:
  211. return "CLS_H_ULP";
  212. case CLS_AB:
  213. return "CLS_AB";
  214. case CLS_AB_HIFI:
  215. return "CLS_AB_HIFI";
  216. default:
  217. return "CLS_H_INVALID";
  218. };
  219. }
  220. static const char *const state_to_str[] = {
  221. [AQT_CLSH_STATE_IDLE] = "STATE_IDLE",
  222. [AQT_CLSH_STATE_HPHL] = "STATE_HPH_L",
  223. [AQT_CLSH_STATE_HPHR] = "STATE_HPH_R",
  224. [AQT_CLSH_STATE_HPH_ST] = "STATE_HPH_ST",
  225. };
  226. static inline void
  227. aqt_enable_clsh_block(struct snd_soc_codec *codec,
  228. struct aqt_clsh_cdc_data *clsh_d, bool enable)
  229. {
  230. if ((enable && ++clsh_d->clsh_users == 1) ||
  231. (!enable && --clsh_d->clsh_users == 0))
  232. snd_soc_update_bits(codec, AQT1000_CDC_CLSH_CRC, 0x01,
  233. (u8) enable);
  234. if (clsh_d->clsh_users < 0)
  235. clsh_d->clsh_users = 0;
  236. dev_dbg(codec->dev, "%s: clsh_users %d, enable %d", __func__,
  237. clsh_d->clsh_users, enable);
  238. }
  239. static inline bool aqt_clsh_enable_status(struct snd_soc_codec *codec)
  240. {
  241. return snd_soc_read(codec, AQT1000_CDC_CLSH_CRC) & 0x01;
  242. }
  243. static inline int aqt_clsh_get_int_mode(struct aqt_clsh_cdc_data *clsh_d,
  244. int clsh_state)
  245. {
  246. int mode;
  247. if ((clsh_state != AQT_CLSH_STATE_HPHL) &&
  248. (clsh_state != AQT_CLSH_STATE_HPHR))
  249. mode = CLS_NONE;
  250. else
  251. mode = clsh_d->interpolator_modes[ffs(clsh_state)];
  252. return mode;
  253. }
  254. static inline void aqt_clsh_set_int_mode(struct aqt_clsh_cdc_data *clsh_d,
  255. int clsh_state, int mode)
  256. {
  257. if ((clsh_state != AQT_CLSH_STATE_HPHL) &&
  258. (clsh_state != AQT_CLSH_STATE_HPHR))
  259. return;
  260. clsh_d->interpolator_modes[ffs(clsh_state)] = mode;
  261. }
  262. static inline void aqt_clsh_set_buck_mode(struct snd_soc_codec *codec,
  263. int mode)
  264. {
  265. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  266. mode == CLS_AB_HIFI || mode == CLS_AB)
  267. snd_soc_update_bits(codec, AQT1000_ANA_RX_SUPPLIES,
  268. 0x08, 0x08); /* set to HIFI */
  269. else
  270. snd_soc_update_bits(codec, AQT1000_ANA_RX_SUPPLIES,
  271. 0x08, 0x00); /* set to default */
  272. }
  273. static inline void aqt_clsh_set_flyback_mode(struct snd_soc_codec *codec,
  274. int mode)
  275. {
  276. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  277. mode == CLS_AB_HIFI || mode == CLS_AB)
  278. snd_soc_update_bits(codec, AQT1000_ANA_RX_SUPPLIES,
  279. 0x04, 0x04); /* set to HIFI */
  280. else
  281. snd_soc_update_bits(codec, AQT1000_ANA_RX_SUPPLIES,
  282. 0x04, 0x00); /* set to Default */
  283. }
  284. static inline void aqt_clsh_gm3_boost_disable(struct snd_soc_codec *codec,
  285. int mode)
  286. {
  287. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  288. mode == CLS_AB_HIFI || mode == CLS_AB) {
  289. snd_soc_update_bits(codec, AQT1000_HPH_CNP_WG_CTL,
  290. 0x80, 0x0); /* disable GM3 Boost */
  291. snd_soc_update_bits(codec, AQT1000_FLYBACK_VNEG_CTRL_4,
  292. 0xF0, 0x80);
  293. } else {
  294. snd_soc_update_bits(codec, AQT1000_HPH_CNP_WG_CTL,
  295. 0x80, 0x80); /* set to Default */
  296. snd_soc_update_bits(codec, AQT1000_FLYBACK_VNEG_CTRL_4,
  297. 0xF0, 0x70);
  298. }
  299. }
  300. static inline void aqt_clsh_flyback_dac_ctl(struct snd_soc_codec *codec,
  301. int vref)
  302. {
  303. snd_soc_update_bits(codec, AQT1000_FLYBACK_VNEGDAC_CTRL_2,
  304. 0xE0, (vref << 5));
  305. }
  306. static inline void aqt_clsh_mode_vref_ctl(struct snd_soc_codec *codec,
  307. int vref_ctl)
  308. {
  309. if (vref_ctl == I2C) {
  310. snd_soc_update_bits(codec, AQT1000_CLASSH_MODE_3, 0x02, 0x02);
  311. snd_soc_update_bits(codec, AQT1000_CLASSH_MODE_2, 0xFF, 0x1C);
  312. } else {
  313. snd_soc_update_bits(codec, AQT1000_CLASSH_MODE_2, 0xFF, 0x3A);
  314. snd_soc_update_bits(codec, AQT1000_CLASSH_MODE_3, 0x02, 0x00);
  315. }
  316. }
  317. static inline void aqt_clsh_buck_current_bias_ctl(struct snd_soc_codec *codec,
  318. bool enable)
  319. {
  320. if (enable) {
  321. snd_soc_update_bits(codec, AQT1000_BUCK_5V_IBIAS_CTL_4,
  322. 0x70, (I_2UA << 4));
  323. snd_soc_update_bits(codec, AQT1000_BUCK_5V_IBIAS_CTL_4,
  324. 0x07, I_0P25UA);
  325. snd_soc_update_bits(codec, AQT1000_BUCK_5V_CTRL_CCL_2,
  326. 0x3F, 0x3F);
  327. } else {
  328. snd_soc_update_bits(codec, AQT1000_BUCK_5V_IBIAS_CTL_4,
  329. 0x70, (I_1UA << 4));
  330. snd_soc_update_bits(codec, AQT1000_BUCK_5V_IBIAS_CTL_4,
  331. 0x07, I_1UA);
  332. snd_soc_update_bits(codec, AQT1000_BUCK_5V_CTRL_CCL_2,
  333. 0x3F, 0x20);
  334. }
  335. }
  336. static inline void aqt_clsh_rdac_hd2_ctl(struct snd_soc_codec *codec,
  337. u8 hd2_div_ctl, u8 state)
  338. {
  339. u16 reg = 0;
  340. if (state == AQT_CLSH_STATE_HPHL)
  341. reg = AQT1000_HPH_NEW_INT_RDAC_HD2_CTL_L;
  342. else if (state == AQT_CLSH_STATE_HPHR)
  343. reg = AQT1000_HPH_NEW_INT_RDAC_HD2_CTL_R;
  344. else
  345. dev_err(codec->dev, "%s: Invalid state: %d\n",
  346. __func__, state);
  347. if (!reg)
  348. snd_soc_update_bits(codec, reg, 0x0F, hd2_div_ctl);
  349. }
  350. static inline void aqt_clsh_force_iq_ctl(struct snd_soc_codec *codec,
  351. int mode)
  352. {
  353. if (mode == CLS_H_LOHIFI || mode == CLS_AB) {
  354. snd_soc_update_bits(codec, AQT1000_HPH_NEW_INT_PA_MISC2,
  355. 0x20, 0x20);
  356. snd_soc_update_bits(codec, AQT1000_RX_BIAS_HPH_LOWPOWER,
  357. 0xF0, 0xC0);
  358. snd_soc_update_bits(codec, AQT1000_HPH_PA_CTL1,
  359. 0x0E, 0x02);
  360. } else {
  361. snd_soc_update_bits(codec, AQT1000_HPH_NEW_INT_PA_MISC2,
  362. 0x20, 0x0);
  363. snd_soc_update_bits(codec, AQT1000_RX_BIAS_HPH_LOWPOWER,
  364. 0xF0, 0x80);
  365. snd_soc_update_bits(codec, AQT1000_HPH_PA_CTL1,
  366. 0x0E, 0x06);
  367. }
  368. }
  369. static void aqt_clsh_buck_ctrl(struct snd_soc_codec *codec,
  370. struct aqt_clsh_cdc_data *clsh_d,
  371. int mode,
  372. bool enable)
  373. {
  374. /* enable/disable buck */
  375. if ((enable && (++clsh_d->buck_users == 1)) ||
  376. (!enable && (--clsh_d->buck_users == 0)))
  377. snd_soc_update_bits(codec, AQT1000_ANA_RX_SUPPLIES,
  378. (1 << 7), (enable << 7));
  379. dev_dbg(codec->dev, "%s: buck_users %d, enable %d, mode: %s",
  380. __func__, clsh_d->buck_users, enable, mode_to_str(mode));
  381. /*
  382. * 500us sleep is required after buck enable/disable
  383. * as per HW requirement
  384. */
  385. usleep_range(500, 500 + AQT_USLEEP_RANGE);
  386. }
  387. static void aqt_clsh_flyback_ctrl(struct snd_soc_codec *codec,
  388. struct aqt_clsh_cdc_data *clsh_d,
  389. int mode,
  390. bool enable)
  391. {
  392. /* enable/disable flyback */
  393. if ((enable && (++clsh_d->flyback_users == 1)) ||
  394. (!enable && (--clsh_d->flyback_users == 0))) {
  395. snd_soc_update_bits(codec, AQT1000_ANA_RX_SUPPLIES,
  396. (1 << 6), (enable << 6));
  397. /* 100usec delay is needed as per HW requirement */
  398. usleep_range(100, 110);
  399. }
  400. dev_dbg(codec->dev, "%s: flyback_users %d, enable %d, mode: %s",
  401. __func__, clsh_d->flyback_users, enable, mode_to_str(mode));
  402. /*
  403. * 500us sleep is required after flyback enable/disable
  404. * as per HW requirement
  405. */
  406. usleep_range(500, 500 + AQT_USLEEP_RANGE);
  407. }
  408. static void aqt_clsh_set_hph_mode(struct snd_soc_codec *codec,
  409. int mode)
  410. {
  411. u8 val = 0;
  412. u8 gain = 0;
  413. u8 res_val = VREF_FILT_R_0OHM;
  414. u8 ipeak = DELTA_I_50MA;
  415. switch (mode) {
  416. case CLS_H_NORMAL:
  417. res_val = VREF_FILT_R_50KOHM;
  418. val = 0x00;
  419. gain = DAC_GAIN_0DB;
  420. ipeak = DELTA_I_50MA;
  421. break;
  422. case CLS_AB:
  423. val = 0x00;
  424. gain = DAC_GAIN_0DB;
  425. ipeak = DELTA_I_50MA;
  426. break;
  427. case CLS_AB_HIFI:
  428. val = 0x08;
  429. break;
  430. case CLS_H_HIFI:
  431. val = 0x08;
  432. gain = DAC_GAIN_M0P2DB;
  433. ipeak = DELTA_I_50MA;
  434. break;
  435. case CLS_H_LOHIFI:
  436. val = 0x00;
  437. break;
  438. case CLS_H_ULP:
  439. val = 0x0C;
  440. break;
  441. case CLS_H_LP:
  442. val = 0x04;
  443. ipeak = DELTA_I_30MA;
  444. break;
  445. default:
  446. return;
  447. };
  448. if (mode == CLS_H_LOHIFI || mode == CLS_AB)
  449. val = 0x04;
  450. snd_soc_update_bits(codec, AQT1000_ANA_HPH, 0x0C, val);
  451. }
  452. static void aqt_clsh_set_buck_regulator_mode(struct snd_soc_codec *codec,
  453. int mode)
  454. {
  455. snd_soc_update_bits(codec, AQT1000_ANA_RX_SUPPLIES,
  456. 0x02, 0x00);
  457. }
  458. static void aqt_clsh_state_hph_st(struct snd_soc_codec *codec,
  459. struct aqt_clsh_cdc_data *clsh_d,
  460. u8 req_state, bool is_enable, int mode)
  461. {
  462. dev_dbg(codec->dev, "%s: mode: %s, %s\n", __func__, mode_to_str(mode),
  463. is_enable ? "enable" : "disable");
  464. if (mode == CLS_AB || mode == CLS_AB_HIFI)
  465. return;
  466. if (is_enable) {
  467. if (req_state == AQT_CLSH_STATE_HPHL)
  468. snd_soc_update_bits(codec,
  469. AQT1000_CDC_RX1_RX_PATH_CFG0,
  470. 0x40, 0x40);
  471. if (req_state == AQT_CLSH_STATE_HPHR)
  472. snd_soc_update_bits(codec,
  473. AQT1000_CDC_RX2_RX_PATH_CFG0,
  474. 0x40, 0x40);
  475. } else {
  476. if (req_state == AQT_CLSH_STATE_HPHL)
  477. snd_soc_update_bits(codec,
  478. AQT1000_CDC_RX1_RX_PATH_CFG0,
  479. 0x40, 0x00);
  480. if (req_state == AQT_CLSH_STATE_HPHR)
  481. snd_soc_update_bits(codec,
  482. AQT1000_CDC_RX2_RX_PATH_CFG0,
  483. 0x40, 0x00);
  484. }
  485. }
  486. static void aqt_clsh_state_hph_r(struct snd_soc_codec *codec,
  487. struct aqt_clsh_cdc_data *clsh_d,
  488. u8 req_state, bool is_enable, int mode)
  489. {
  490. dev_dbg(codec->dev, "%s: mode: %s, %s\n", __func__, mode_to_str(mode),
  491. is_enable ? "enable" : "disable");
  492. if (mode == CLS_H_NORMAL) {
  493. dev_err(codec->dev, "%s: Normal mode not applicable for hph_r\n",
  494. __func__);
  495. return;
  496. }
  497. if (is_enable) {
  498. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  499. aqt_enable_clsh_block(codec, clsh_d, true);
  500. /*
  501. * These K1 values depend on the Headphone Impedance
  502. * For now it is assumed to be 16 ohm
  503. */
  504. snd_soc_update_bits(codec, AQT1000_CDC_CLSH_K1_MSB,
  505. 0x0F, 0x00);
  506. snd_soc_update_bits(codec, AQT1000_CDC_CLSH_K1_LSB,
  507. 0xFF, 0xC0);
  508. snd_soc_update_bits(codec,
  509. AQT1000_CDC_RX2_RX_PATH_CFG0,
  510. 0x40, 0x40);
  511. }
  512. aqt_clsh_set_buck_regulator_mode(codec, mode);
  513. aqt_clsh_set_flyback_mode(codec, mode);
  514. aqt_clsh_gm3_boost_disable(codec, mode);
  515. aqt_clsh_flyback_dac_ctl(codec, VREF_N0P9V);
  516. aqt_clsh_mode_vref_ctl(codec, I2C);
  517. aqt_clsh_force_iq_ctl(codec, mode);
  518. aqt_clsh_rdac_hd2_ctl(codec, P82_0P77, req_state);
  519. aqt_clsh_flyback_ctrl(codec, clsh_d, mode, true);
  520. aqt_clsh_flyback_dac_ctl(codec, VREF_N1P827V);
  521. aqt_clsh_set_buck_mode(codec, mode);
  522. aqt_clsh_buck_ctrl(codec, clsh_d, mode, true);
  523. aqt_clsh_mode_vref_ctl(codec, CONTROLLER);
  524. aqt_clsh_buck_current_bias_ctl(codec, true);
  525. aqt_clsh_set_hph_mode(codec, mode);
  526. } else {
  527. aqt_clsh_set_hph_mode(codec, CLS_H_NORMAL);
  528. aqt_clsh_buck_current_bias_ctl(codec, false);
  529. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  530. snd_soc_update_bits(codec,
  531. AQT1000_CDC_RX2_RX_PATH_CFG0,
  532. 0x40, 0x00);
  533. aqt_enable_clsh_block(codec, clsh_d, false);
  534. }
  535. /* buck and flyback set to default mode and disable */
  536. aqt_clsh_buck_ctrl(codec, clsh_d, CLS_H_NORMAL, false);
  537. aqt_clsh_flyback_ctrl(codec, clsh_d, CLS_H_NORMAL, false);
  538. aqt_clsh_rdac_hd2_ctl(codec, P5_0P35, req_state);
  539. aqt_clsh_force_iq_ctl(codec, CLS_H_NORMAL);
  540. aqt_clsh_gm3_boost_disable(codec, CLS_H_NORMAL);
  541. aqt_clsh_set_flyback_mode(codec, CLS_H_NORMAL);
  542. aqt_clsh_set_buck_mode(codec, CLS_H_NORMAL);
  543. aqt_clsh_set_buck_regulator_mode(codec, CLS_H_NORMAL);
  544. }
  545. }
  546. static void aqt_clsh_state_hph_l(struct snd_soc_codec *codec,
  547. struct aqt_clsh_cdc_data *clsh_d,
  548. u8 req_state, bool is_enable, int mode)
  549. {
  550. dev_dbg(codec->dev, "%s: mode: %s, %s\n", __func__, mode_to_str(mode),
  551. is_enable ? "enable" : "disable");
  552. if (mode == CLS_H_NORMAL) {
  553. dev_err(codec->dev, "%s: Normal mode not applicable for hph_l\n",
  554. __func__);
  555. return;
  556. }
  557. if (is_enable) {
  558. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  559. aqt_enable_clsh_block(codec, clsh_d, true);
  560. /*
  561. * These K1 values depend on the Headphone Impedance
  562. * For now it is assumed to be 16 ohm
  563. */
  564. snd_soc_update_bits(codec, AQT1000_CDC_CLSH_K1_MSB,
  565. 0x0F, 0x00);
  566. snd_soc_update_bits(codec, AQT1000_CDC_CLSH_K1_LSB,
  567. 0xFF, 0xC0);
  568. snd_soc_update_bits(codec,
  569. AQT1000_CDC_RX1_RX_PATH_CFG0,
  570. 0x40, 0x40);
  571. }
  572. aqt_clsh_set_buck_regulator_mode(codec, mode);
  573. aqt_clsh_set_flyback_mode(codec, mode);
  574. aqt_clsh_gm3_boost_disable(codec, mode);
  575. aqt_clsh_flyback_dac_ctl(codec, VREF_N0P9V);
  576. aqt_clsh_mode_vref_ctl(codec, I2C);
  577. aqt_clsh_force_iq_ctl(codec, mode);
  578. aqt_clsh_rdac_hd2_ctl(codec, P82_0P77, req_state);
  579. aqt_clsh_flyback_ctrl(codec, clsh_d, mode, true);
  580. aqt_clsh_flyback_dac_ctl(codec, VREF_N1P827V);
  581. aqt_clsh_set_buck_mode(codec, mode);
  582. aqt_clsh_buck_ctrl(codec, clsh_d, mode, true);
  583. aqt_clsh_mode_vref_ctl(codec, CONTROLLER);
  584. aqt_clsh_buck_current_bias_ctl(codec, true);
  585. aqt_clsh_set_hph_mode(codec, mode);
  586. } else {
  587. aqt_clsh_set_hph_mode(codec, CLS_H_NORMAL);
  588. aqt_clsh_buck_current_bias_ctl(codec, false);
  589. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  590. snd_soc_update_bits(codec,
  591. AQT1000_CDC_RX1_RX_PATH_CFG0,
  592. 0x40, 0x00);
  593. aqt_enable_clsh_block(codec, clsh_d, false);
  594. }
  595. /* set buck and flyback to Default Mode */
  596. aqt_clsh_buck_ctrl(codec, clsh_d, CLS_H_NORMAL, false);
  597. aqt_clsh_flyback_ctrl(codec, clsh_d, CLS_H_NORMAL, false);
  598. aqt_clsh_rdac_hd2_ctl(codec, P5_0P35, req_state);
  599. aqt_clsh_force_iq_ctl(codec, CLS_H_NORMAL);
  600. aqt_clsh_gm3_boost_disable(codec, CLS_H_NORMAL);
  601. aqt_clsh_set_flyback_mode(codec, CLS_H_NORMAL);
  602. aqt_clsh_set_buck_mode(codec, CLS_H_NORMAL);
  603. aqt_clsh_set_buck_regulator_mode(codec, CLS_H_NORMAL);
  604. }
  605. }
  606. static void aqt_clsh_state_err(struct snd_soc_codec *codec,
  607. struct aqt_clsh_cdc_data *clsh_d,
  608. u8 req_state, bool is_enable, int mode)
  609. {
  610. dev_err(codec->dev,
  611. "%s Wrong request for class H state machine requested to %s %s",
  612. __func__, is_enable ? "enable" : "disable",
  613. state_to_str[req_state]);
  614. }
  615. /*
  616. * Function: aqt_clsh_is_state_valid
  617. * Params: state
  618. * Description:
  619. * Provides information on valid states of Class H configuration
  620. */
  621. static bool aqt_clsh_is_state_valid(u8 state)
  622. {
  623. switch (state) {
  624. case AQT_CLSH_STATE_IDLE:
  625. case AQT_CLSH_STATE_HPHL:
  626. case AQT_CLSH_STATE_HPHR:
  627. case AQT_CLSH_STATE_HPH_ST:
  628. return true;
  629. default:
  630. return false;
  631. };
  632. }
  633. /*
  634. * Function: aqt_clsh_fsm
  635. * Params: codec, cdc_clsh_d, req_state, req_type, clsh_event
  636. * Description:
  637. * This function handles PRE DAC and POST DAC conditions of different devices
  638. * and updates class H configuration of different combination of devices
  639. * based on validity of their states. cdc_clsh_d will contain current
  640. * class h state information
  641. */
  642. void aqt_clsh_fsm(struct snd_soc_codec *codec,
  643. struct aqt_clsh_cdc_data *cdc_clsh_d,
  644. u8 clsh_event, u8 req_state,
  645. int int_mode)
  646. {
  647. u8 old_state, new_state;
  648. switch (clsh_event) {
  649. case AQT_CLSH_EVENT_PRE_DAC:
  650. old_state = cdc_clsh_d->state;
  651. new_state = old_state | req_state;
  652. if (!aqt_clsh_is_state_valid(new_state)) {
  653. dev_err(codec->dev,
  654. "%s: Class-H not a valid new state: %s\n",
  655. __func__, state_to_str[new_state]);
  656. return;
  657. }
  658. if (new_state == old_state) {
  659. dev_err(codec->dev,
  660. "%s: Class-H already in requested state: %s\n",
  661. __func__, state_to_str[new_state]);
  662. return;
  663. }
  664. cdc_clsh_d->state = new_state;
  665. aqt_clsh_set_int_mode(cdc_clsh_d, req_state, int_mode);
  666. (*clsh_state_fp[new_state]) (codec, cdc_clsh_d, req_state,
  667. CLSH_REQ_ENABLE, int_mode);
  668. dev_dbg(codec->dev,
  669. "%s: ClassH state transition from %s to %s\n",
  670. __func__, state_to_str[old_state],
  671. state_to_str[cdc_clsh_d->state]);
  672. break;
  673. case AQT_CLSH_EVENT_POST_PA:
  674. old_state = cdc_clsh_d->state;
  675. new_state = old_state & (~req_state);
  676. if (new_state < NUM_CLSH_STATES) {
  677. if (!aqt_clsh_is_state_valid(old_state)) {
  678. dev_err(codec->dev,
  679. "%s:Invalid old state:%s\n",
  680. __func__, state_to_str[old_state]);
  681. return;
  682. }
  683. if (new_state == old_state) {
  684. dev_err(codec->dev,
  685. "%s: Class-H already in requested state: %s\n",
  686. __func__,state_to_str[new_state]);
  687. return;
  688. }
  689. (*clsh_state_fp[old_state]) (codec, cdc_clsh_d,
  690. req_state, CLSH_REQ_DISABLE,
  691. int_mode);
  692. cdc_clsh_d->state = new_state;
  693. aqt_clsh_set_int_mode(cdc_clsh_d, req_state, CLS_NONE);
  694. dev_dbg(codec->dev, "%s: ClassH state transition from %s to %s\n",
  695. __func__, state_to_str[old_state],
  696. state_to_str[cdc_clsh_d->state]);
  697. }
  698. break;
  699. };
  700. }
  701. EXPORT_SYMBOL(aqt_clsh_fsm);
  702. /*
  703. * Function: aqt_clsh_get_clsh_state
  704. * Params: clsh
  705. * Description:
  706. * This function returns the state of the class H controller
  707. */
  708. int aqt_clsh_get_clsh_state(struct aqt_clsh_cdc_data *clsh)
  709. {
  710. return clsh->state;
  711. }
  712. EXPORT_SYMBOL(aqt_clsh_get_clsh_state);
  713. /*
  714. * Function: aqt_clsh_init
  715. * Params: clsh
  716. * Description:
  717. * This function initializes the class H controller
  718. */
  719. void aqt_clsh_init(struct aqt_clsh_cdc_data *clsh)
  720. {
  721. int i;
  722. clsh->state = AQT_CLSH_STATE_IDLE;
  723. for (i = 0; i < NUM_CLSH_STATES; i++)
  724. clsh_state_fp[i] = aqt_clsh_state_err;
  725. clsh_state_fp[AQT_CLSH_STATE_HPHL] = aqt_clsh_state_hph_l;
  726. clsh_state_fp[AQT_CLSH_STATE_HPHR] = aqt_clsh_state_hph_r;
  727. clsh_state_fp[AQT_CLSH_STATE_HPH_ST] = aqt_clsh_state_hph_st;
  728. /* Set interpolator modes to NONE */
  729. aqt_clsh_set_int_mode(clsh, AQT_CLSH_STATE_HPHL, CLS_NONE);
  730. aqt_clsh_set_int_mode(clsh, AQT_CLSH_STATE_HPHR, CLS_NONE);
  731. clsh->flyback_users = 0;
  732. clsh->buck_users = 0;
  733. clsh->clsh_users = 0;
  734. }
  735. EXPORT_SYMBOL(aqt_clsh_init);