aqt1000-clsh.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. // SPDX-License-Identifier: GPL-2.0-only
  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_component *,
  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: component, 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_component *component,
  166. int imped, bool reset)
  167. {
  168. int i;
  169. int index = 0;
  170. int table_size;
  171. static const struct aqt_reg_mask_val
  172. (*imped_table_ptr)[MAX_IMPED_PARAMS];
  173. table_size = ARRAY_SIZE(imped_table_aqt);
  174. imped_table_ptr = imped_table_aqt;
  175. /* reset = 1, which means request is to reset the register values */
  176. if (reset) {
  177. for (i = 0; i < MAX_IMPED_PARAMS; i++)
  178. snd_soc_component_update_bits(component,
  179. imped_table_ptr[index][i].reg,
  180. imped_table_ptr[index][i].mask, 0);
  181. return;
  182. }
  183. index = get_impedance_index(imped);
  184. if (index >= (ARRAY_SIZE(imped_index) - 1)) {
  185. pr_debug("%s, impedance not in range = %d\n", __func__, imped);
  186. return;
  187. }
  188. if (index >= table_size) {
  189. pr_debug("%s, impedance index not in range = %d\n", __func__,
  190. index);
  191. return;
  192. }
  193. for (i = 0; i < MAX_IMPED_PARAMS; i++)
  194. snd_soc_component_update_bits(component,
  195. imped_table_ptr[index][i].reg,
  196. imped_table_ptr[index][i].mask,
  197. imped_table_ptr[index][i].val);
  198. }
  199. EXPORT_SYMBOL(aqt_clsh_imped_config);
  200. static const char *mode_to_str(int mode)
  201. {
  202. switch (mode) {
  203. case CLS_H_NORMAL:
  204. return "CLS_H_NORMAL";
  205. case CLS_H_HIFI:
  206. return "CLS_H_HIFI";
  207. case CLS_H_LOHIFI:
  208. return "CLS_H_LOHIFI";
  209. case CLS_H_LP:
  210. return "CLS_H_LP";
  211. case CLS_H_ULP:
  212. return "CLS_H_ULP";
  213. case CLS_AB:
  214. return "CLS_AB";
  215. case CLS_AB_HIFI:
  216. return "CLS_AB_HIFI";
  217. default:
  218. return "CLS_H_INVALID";
  219. };
  220. }
  221. static const char *const state_to_str[] = {
  222. [AQT_CLSH_STATE_IDLE] = "STATE_IDLE",
  223. [AQT_CLSH_STATE_HPHL] = "STATE_HPH_L",
  224. [AQT_CLSH_STATE_HPHR] = "STATE_HPH_R",
  225. [AQT_CLSH_STATE_HPH_ST] = "STATE_HPH_ST",
  226. };
  227. static inline void
  228. aqt_enable_clsh_block(struct snd_soc_component *component,
  229. struct aqt_clsh_cdc_data *clsh_d, bool enable)
  230. {
  231. if ((enable && ++clsh_d->clsh_users == 1) ||
  232. (!enable && --clsh_d->clsh_users == 0))
  233. snd_soc_component_update_bits(component, AQT1000_CDC_CLSH_CRC,
  234. 0x01, (u8) enable);
  235. if (clsh_d->clsh_users < 0)
  236. clsh_d->clsh_users = 0;
  237. dev_dbg(component->dev, "%s: clsh_users %d, enable %d", __func__,
  238. clsh_d->clsh_users, enable);
  239. }
  240. static inline bool aqt_clsh_enable_status(struct snd_soc_component *component)
  241. {
  242. return snd_soc_component_read32(
  243. component, AQT1000_CDC_CLSH_CRC) & 0x01;
  244. }
  245. static inline int aqt_clsh_get_int_mode(struct aqt_clsh_cdc_data *clsh_d,
  246. int clsh_state)
  247. {
  248. int mode;
  249. if ((clsh_state != AQT_CLSH_STATE_HPHL) &&
  250. (clsh_state != AQT_CLSH_STATE_HPHR))
  251. mode = CLS_NONE;
  252. else
  253. mode = clsh_d->interpolator_modes[ffs(clsh_state)];
  254. return mode;
  255. }
  256. static inline void aqt_clsh_set_int_mode(struct aqt_clsh_cdc_data *clsh_d,
  257. int clsh_state, int mode)
  258. {
  259. if ((clsh_state != AQT_CLSH_STATE_HPHL) &&
  260. (clsh_state != AQT_CLSH_STATE_HPHR))
  261. return;
  262. clsh_d->interpolator_modes[ffs(clsh_state)] = mode;
  263. }
  264. static inline void aqt_clsh_set_buck_mode(struct snd_soc_component *component,
  265. int mode)
  266. {
  267. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  268. mode == CLS_AB_HIFI || mode == CLS_AB)
  269. snd_soc_component_update_bits(component,
  270. AQT1000_ANA_RX_SUPPLIES,
  271. 0x08, 0x08); /* set to HIFI */
  272. else
  273. snd_soc_component_update_bits(component,
  274. AQT1000_ANA_RX_SUPPLIES,
  275. 0x08, 0x00); /* set to default */
  276. }
  277. static inline void aqt_clsh_set_flyback_mode(
  278. struct snd_soc_component *component, int mode)
  279. {
  280. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  281. mode == CLS_AB_HIFI || mode == CLS_AB)
  282. snd_soc_component_update_bits(component,
  283. AQT1000_ANA_RX_SUPPLIES,
  284. 0x04, 0x04); /* set to HIFI */
  285. else
  286. snd_soc_component_update_bits(component,
  287. AQT1000_ANA_RX_SUPPLIES,
  288. 0x04, 0x00); /* set to Default */
  289. }
  290. static inline void aqt_clsh_gm3_boost_disable(
  291. struct snd_soc_component *component, int mode)
  292. {
  293. if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI ||
  294. mode == CLS_AB_HIFI || mode == CLS_AB) {
  295. snd_soc_component_update_bits(component,
  296. AQT1000_HPH_CNP_WG_CTL,
  297. 0x80, 0x0); /* disable GM3 Boost */
  298. snd_soc_component_update_bits(component,
  299. AQT1000_FLYBACK_VNEG_CTRL_4,
  300. 0xF0, 0x80);
  301. } else {
  302. snd_soc_component_update_bits(component,
  303. AQT1000_HPH_CNP_WG_CTL,
  304. 0x80, 0x80); /* set to Default */
  305. snd_soc_component_update_bits(component,
  306. AQT1000_FLYBACK_VNEG_CTRL_4,
  307. 0xF0, 0x70);
  308. }
  309. }
  310. static inline void aqt_clsh_flyback_dac_ctl(
  311. struct snd_soc_component *component, int vref)
  312. {
  313. snd_soc_component_update_bits(component,
  314. AQT1000_FLYBACK_VNEGDAC_CTRL_2,
  315. 0xE0, (vref << 5));
  316. }
  317. static inline void aqt_clsh_mode_vref_ctl(struct snd_soc_component *component,
  318. int vref_ctl)
  319. {
  320. if (vref_ctl == I2C) {
  321. snd_soc_component_update_bits(component, AQT1000_CLASSH_MODE_3,
  322. 0x02, 0x02);
  323. snd_soc_component_update_bits(component, AQT1000_CLASSH_MODE_2,
  324. 0xFF, 0x1C);
  325. } else {
  326. snd_soc_component_update_bits(component, AQT1000_CLASSH_MODE_2,
  327. 0xFF, 0x3A);
  328. snd_soc_component_update_bits(component, AQT1000_CLASSH_MODE_3,
  329. 0x02, 0x00);
  330. }
  331. }
  332. static inline void aqt_clsh_buck_current_bias_ctl(
  333. struct snd_soc_component *component, bool enable)
  334. {
  335. if (enable) {
  336. snd_soc_component_update_bits(component,
  337. AQT1000_BUCK_5V_IBIAS_CTL_4,
  338. 0x70, (I_2UA << 4));
  339. snd_soc_component_update_bits(component,
  340. AQT1000_BUCK_5V_IBIAS_CTL_4,
  341. 0x07, I_0P25UA);
  342. snd_soc_component_update_bits(component,
  343. AQT1000_BUCK_5V_CTRL_CCL_2,
  344. 0x3F, 0x3F);
  345. } else {
  346. snd_soc_component_update_bits(component,
  347. AQT1000_BUCK_5V_IBIAS_CTL_4,
  348. 0x70, (I_1UA << 4));
  349. snd_soc_component_update_bits(component,
  350. AQT1000_BUCK_5V_IBIAS_CTL_4,
  351. 0x07, I_1UA);
  352. snd_soc_component_update_bits(component,
  353. AQT1000_BUCK_5V_CTRL_CCL_2,
  354. 0x3F, 0x20);
  355. }
  356. }
  357. static inline void aqt_clsh_rdac_hd2_ctl(struct snd_soc_component *component,
  358. u8 hd2_div_ctl, u8 state)
  359. {
  360. u16 reg = 0;
  361. if (state == AQT_CLSH_STATE_HPHL)
  362. reg = AQT1000_HPH_NEW_INT_RDAC_HD2_CTL_L;
  363. else if (state == AQT_CLSH_STATE_HPHR)
  364. reg = AQT1000_HPH_NEW_INT_RDAC_HD2_CTL_R;
  365. else
  366. dev_err(component->dev, "%s: Invalid state: %d\n",
  367. __func__, state);
  368. if (!reg)
  369. snd_soc_component_update_bits(component, reg,
  370. 0x0F, hd2_div_ctl);
  371. }
  372. static inline void aqt_clsh_force_iq_ctl(struct snd_soc_component *component,
  373. int mode)
  374. {
  375. if (mode == CLS_H_LOHIFI || mode == CLS_AB) {
  376. snd_soc_component_update_bits(component,
  377. AQT1000_HPH_NEW_INT_PA_MISC2,
  378. 0x20, 0x20);
  379. snd_soc_component_update_bits(component,
  380. AQT1000_RX_BIAS_HPH_LOWPOWER,
  381. 0xF0, 0xC0);
  382. snd_soc_component_update_bits(component,
  383. AQT1000_HPH_PA_CTL1,
  384. 0x0E, 0x02);
  385. } else {
  386. snd_soc_component_update_bits(component,
  387. AQT1000_HPH_NEW_INT_PA_MISC2,
  388. 0x20, 0x0);
  389. snd_soc_component_update_bits(component,
  390. AQT1000_RX_BIAS_HPH_LOWPOWER,
  391. 0xF0, 0x80);
  392. snd_soc_component_update_bits(component,
  393. AQT1000_HPH_PA_CTL1,
  394. 0x0E, 0x06);
  395. }
  396. }
  397. static void aqt_clsh_buck_ctrl(struct snd_soc_component *component,
  398. struct aqt_clsh_cdc_data *clsh_d,
  399. int mode,
  400. bool enable)
  401. {
  402. /* enable/disable buck */
  403. if ((enable && (++clsh_d->buck_users == 1)) ||
  404. (!enable && (--clsh_d->buck_users == 0)))
  405. snd_soc_component_update_bits(component,
  406. AQT1000_ANA_RX_SUPPLIES,
  407. (1 << 7), (enable << 7));
  408. dev_dbg(component->dev, "%s: buck_users %d, enable %d, mode: %s",
  409. __func__, clsh_d->buck_users, enable, mode_to_str(mode));
  410. /*
  411. * 500us sleep is required after buck enable/disable
  412. * as per HW requirement
  413. */
  414. usleep_range(500, 500 + AQT_USLEEP_RANGE);
  415. }
  416. static void aqt_clsh_flyback_ctrl(struct snd_soc_component *component,
  417. struct aqt_clsh_cdc_data *clsh_d,
  418. int mode,
  419. bool enable)
  420. {
  421. /* enable/disable flyback */
  422. if ((enable && (++clsh_d->flyback_users == 1)) ||
  423. (!enable && (--clsh_d->flyback_users == 0))) {
  424. snd_soc_component_update_bits(component,
  425. AQT1000_ANA_RX_SUPPLIES,
  426. (1 << 6), (enable << 6));
  427. /* 100usec delay is needed as per HW requirement */
  428. usleep_range(100, 110);
  429. }
  430. dev_dbg(component->dev, "%s: flyback_users %d, enable %d, mode: %s",
  431. __func__, clsh_d->flyback_users, enable, mode_to_str(mode));
  432. /*
  433. * 500us sleep is required after flyback enable/disable
  434. * as per HW requirement
  435. */
  436. usleep_range(500, 500 + AQT_USLEEP_RANGE);
  437. }
  438. static void aqt_clsh_set_hph_mode(struct snd_soc_component *component,
  439. int mode)
  440. {
  441. u8 val = 0;
  442. u8 gain = 0;
  443. u8 res_val = VREF_FILT_R_0OHM;
  444. u8 ipeak = DELTA_I_50MA;
  445. switch (mode) {
  446. case CLS_H_NORMAL:
  447. res_val = VREF_FILT_R_50KOHM;
  448. val = 0x00;
  449. gain = DAC_GAIN_0DB;
  450. ipeak = DELTA_I_50MA;
  451. break;
  452. case CLS_AB:
  453. val = 0x00;
  454. gain = DAC_GAIN_0DB;
  455. ipeak = DELTA_I_50MA;
  456. break;
  457. case CLS_AB_HIFI:
  458. val = 0x08;
  459. break;
  460. case CLS_H_HIFI:
  461. val = 0x08;
  462. gain = DAC_GAIN_M0P2DB;
  463. ipeak = DELTA_I_50MA;
  464. break;
  465. case CLS_H_LOHIFI:
  466. val = 0x00;
  467. break;
  468. case CLS_H_ULP:
  469. val = 0x0C;
  470. break;
  471. case CLS_H_LP:
  472. val = 0x04;
  473. ipeak = DELTA_I_30MA;
  474. break;
  475. default:
  476. return;
  477. };
  478. if (mode == CLS_H_LOHIFI || mode == CLS_AB)
  479. val = 0x04;
  480. snd_soc_component_update_bits(component, AQT1000_ANA_HPH, 0x0C, val);
  481. }
  482. static void aqt_clsh_set_buck_regulator_mode(
  483. struct snd_soc_component *component, int mode)
  484. {
  485. snd_soc_component_update_bits(component, AQT1000_ANA_RX_SUPPLIES,
  486. 0x02, 0x00);
  487. }
  488. static void aqt_clsh_state_hph_st(struct snd_soc_component *component,
  489. struct aqt_clsh_cdc_data *clsh_d,
  490. u8 req_state, bool is_enable, int mode)
  491. {
  492. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  493. mode_to_str(mode),
  494. is_enable ? "enable" : "disable");
  495. if (mode == CLS_AB || mode == CLS_AB_HIFI)
  496. return;
  497. if (is_enable) {
  498. if (req_state == AQT_CLSH_STATE_HPHL)
  499. snd_soc_component_update_bits(component,
  500. AQT1000_CDC_RX1_RX_PATH_CFG0,
  501. 0x40, 0x40);
  502. if (req_state == AQT_CLSH_STATE_HPHR)
  503. snd_soc_component_update_bits(component,
  504. AQT1000_CDC_RX2_RX_PATH_CFG0,
  505. 0x40, 0x40);
  506. } else {
  507. if (req_state == AQT_CLSH_STATE_HPHL)
  508. snd_soc_component_update_bits(component,
  509. AQT1000_CDC_RX1_RX_PATH_CFG0,
  510. 0x40, 0x00);
  511. if (req_state == AQT_CLSH_STATE_HPHR)
  512. snd_soc_component_update_bits(component,
  513. AQT1000_CDC_RX2_RX_PATH_CFG0,
  514. 0x40, 0x00);
  515. }
  516. }
  517. static void aqt_clsh_state_hph_r(struct snd_soc_component *component,
  518. struct aqt_clsh_cdc_data *clsh_d,
  519. u8 req_state, bool is_enable, int mode)
  520. {
  521. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  522. mode_to_str(mode),
  523. is_enable ? "enable" : "disable");
  524. if (mode == CLS_H_NORMAL) {
  525. dev_err(component->dev, "%s: Normal mode not applicable for hph_r\n",
  526. __func__);
  527. return;
  528. }
  529. if (is_enable) {
  530. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  531. aqt_enable_clsh_block(component, clsh_d, true);
  532. /*
  533. * These K1 values depend on the Headphone Impedance
  534. * For now it is assumed to be 16 ohm
  535. */
  536. snd_soc_component_update_bits(component,
  537. AQT1000_CDC_CLSH_K1_MSB,
  538. 0x0F, 0x00);
  539. snd_soc_component_update_bits(component,
  540. AQT1000_CDC_CLSH_K1_LSB,
  541. 0xFF, 0xC0);
  542. snd_soc_component_update_bits(component,
  543. AQT1000_CDC_RX2_RX_PATH_CFG0,
  544. 0x40, 0x40);
  545. }
  546. aqt_clsh_set_buck_regulator_mode(component, mode);
  547. aqt_clsh_set_flyback_mode(component, mode);
  548. aqt_clsh_gm3_boost_disable(component, mode);
  549. aqt_clsh_flyback_dac_ctl(component, VREF_N0P9V);
  550. aqt_clsh_mode_vref_ctl(component, I2C);
  551. aqt_clsh_force_iq_ctl(component, mode);
  552. aqt_clsh_rdac_hd2_ctl(component, P82_0P77, req_state);
  553. aqt_clsh_flyback_ctrl(component, clsh_d, mode, true);
  554. aqt_clsh_flyback_dac_ctl(component, VREF_N1P827V);
  555. aqt_clsh_set_buck_mode(component, mode);
  556. aqt_clsh_buck_ctrl(component, clsh_d, mode, true);
  557. aqt_clsh_mode_vref_ctl(component, CONTROLLER);
  558. aqt_clsh_buck_current_bias_ctl(component, true);
  559. aqt_clsh_set_hph_mode(component, mode);
  560. } else {
  561. aqt_clsh_set_hph_mode(component, CLS_H_NORMAL);
  562. aqt_clsh_buck_current_bias_ctl(component, false);
  563. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  564. snd_soc_component_update_bits(component,
  565. AQT1000_CDC_RX2_RX_PATH_CFG0,
  566. 0x40, 0x00);
  567. aqt_enable_clsh_block(component, clsh_d, false);
  568. }
  569. /* buck and flyback set to default mode and disable */
  570. aqt_clsh_buck_ctrl(component, clsh_d, CLS_H_NORMAL, false);
  571. aqt_clsh_flyback_ctrl(component, clsh_d, CLS_H_NORMAL, false);
  572. aqt_clsh_rdac_hd2_ctl(component, P5_0P35, req_state);
  573. aqt_clsh_force_iq_ctl(component, CLS_H_NORMAL);
  574. aqt_clsh_gm3_boost_disable(component, CLS_H_NORMAL);
  575. aqt_clsh_set_flyback_mode(component, CLS_H_NORMAL);
  576. aqt_clsh_set_buck_mode(component, CLS_H_NORMAL);
  577. aqt_clsh_set_buck_regulator_mode(component, CLS_H_NORMAL);
  578. }
  579. }
  580. static void aqt_clsh_state_hph_l(struct snd_soc_component *component,
  581. struct aqt_clsh_cdc_data *clsh_d,
  582. u8 req_state, bool is_enable, int mode)
  583. {
  584. dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__,
  585. mode_to_str(mode), is_enable ? "enable" : "disable");
  586. if (mode == CLS_H_NORMAL) {
  587. dev_err(component->dev, "%s: Normal mode not applicable for hph_l\n",
  588. __func__);
  589. return;
  590. }
  591. if (is_enable) {
  592. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  593. aqt_enable_clsh_block(component, clsh_d, true);
  594. /*
  595. * These K1 values depend on the Headphone Impedance
  596. * For now it is assumed to be 16 ohm
  597. */
  598. snd_soc_component_update_bits(component,
  599. AQT1000_CDC_CLSH_K1_MSB,
  600. 0x0F, 0x00);
  601. snd_soc_component_update_bits(component,
  602. AQT1000_CDC_CLSH_K1_LSB,
  603. 0xFF, 0xC0);
  604. snd_soc_component_update_bits(component,
  605. AQT1000_CDC_RX1_RX_PATH_CFG0,
  606. 0x40, 0x40);
  607. }
  608. aqt_clsh_set_buck_regulator_mode(component, mode);
  609. aqt_clsh_set_flyback_mode(component, mode);
  610. aqt_clsh_gm3_boost_disable(component, mode);
  611. aqt_clsh_flyback_dac_ctl(component, VREF_N0P9V);
  612. aqt_clsh_mode_vref_ctl(component, I2C);
  613. aqt_clsh_force_iq_ctl(component, mode);
  614. aqt_clsh_rdac_hd2_ctl(component, P82_0P77, req_state);
  615. aqt_clsh_flyback_ctrl(component, clsh_d, mode, true);
  616. aqt_clsh_flyback_dac_ctl(component, VREF_N1P827V);
  617. aqt_clsh_set_buck_mode(component, mode);
  618. aqt_clsh_buck_ctrl(component, clsh_d, mode, true);
  619. aqt_clsh_mode_vref_ctl(component, CONTROLLER);
  620. aqt_clsh_buck_current_bias_ctl(component, true);
  621. aqt_clsh_set_hph_mode(component, mode);
  622. } else {
  623. aqt_clsh_set_hph_mode(component, CLS_H_NORMAL);
  624. aqt_clsh_buck_current_bias_ctl(component, false);
  625. if (mode != CLS_AB && mode != CLS_AB_HIFI) {
  626. snd_soc_component_update_bits(component,
  627. AQT1000_CDC_RX1_RX_PATH_CFG0,
  628. 0x40, 0x00);
  629. aqt_enable_clsh_block(component, clsh_d, false);
  630. }
  631. /* set buck and flyback to Default Mode */
  632. aqt_clsh_buck_ctrl(component, clsh_d, CLS_H_NORMAL, false);
  633. aqt_clsh_flyback_ctrl(component, clsh_d, CLS_H_NORMAL, false);
  634. aqt_clsh_rdac_hd2_ctl(component, P5_0P35, req_state);
  635. aqt_clsh_force_iq_ctl(component, CLS_H_NORMAL);
  636. aqt_clsh_gm3_boost_disable(component, CLS_H_NORMAL);
  637. aqt_clsh_set_flyback_mode(component, CLS_H_NORMAL);
  638. aqt_clsh_set_buck_mode(component, CLS_H_NORMAL);
  639. aqt_clsh_set_buck_regulator_mode(component, CLS_H_NORMAL);
  640. }
  641. }
  642. static void aqt_clsh_state_err(struct snd_soc_component *component,
  643. struct aqt_clsh_cdc_data *clsh_d,
  644. u8 req_state, bool is_enable, int mode)
  645. {
  646. dev_err(component->dev,
  647. "%s Wrong request for class H state machine requested to %s %s",
  648. __func__, is_enable ? "enable" : "disable",
  649. state_to_str[req_state]);
  650. }
  651. /*
  652. * Function: aqt_clsh_is_state_valid
  653. * Params: state
  654. * Description:
  655. * Provides information on valid states of Class H configuration
  656. */
  657. static bool aqt_clsh_is_state_valid(u8 state)
  658. {
  659. switch (state) {
  660. case AQT_CLSH_STATE_IDLE:
  661. case AQT_CLSH_STATE_HPHL:
  662. case AQT_CLSH_STATE_HPHR:
  663. case AQT_CLSH_STATE_HPH_ST:
  664. return true;
  665. default:
  666. return false;
  667. };
  668. }
  669. /*
  670. * Function: aqt_clsh_fsm
  671. * Params: component, cdc_clsh_d, req_state, req_type, clsh_event
  672. * Description:
  673. * This function handles PRE DAC and POST DAC conditions of different devices
  674. * and updates class H configuration of different combination of devices
  675. * based on validity of their states. cdc_clsh_d will contain current
  676. * class h state information
  677. */
  678. void aqt_clsh_fsm(struct snd_soc_component *component,
  679. struct aqt_clsh_cdc_data *cdc_clsh_d,
  680. u8 clsh_event, u8 req_state,
  681. int int_mode)
  682. {
  683. u8 old_state, new_state;
  684. switch (clsh_event) {
  685. case AQT_CLSH_EVENT_PRE_DAC:
  686. old_state = cdc_clsh_d->state;
  687. new_state = old_state | req_state;
  688. if (!aqt_clsh_is_state_valid(new_state)) {
  689. dev_err(component->dev,
  690. "%s: Class-H not a valid new state: %s\n",
  691. __func__, state_to_str[new_state]);
  692. return;
  693. }
  694. if (new_state == old_state) {
  695. dev_err(component->dev,
  696. "%s: Class-H already in requested state: %s\n",
  697. __func__, state_to_str[new_state]);
  698. return;
  699. }
  700. cdc_clsh_d->state = new_state;
  701. aqt_clsh_set_int_mode(cdc_clsh_d, req_state, int_mode);
  702. (*clsh_state_fp[new_state]) (component, cdc_clsh_d, req_state,
  703. CLSH_REQ_ENABLE, int_mode);
  704. dev_dbg(component->dev,
  705. "%s: ClassH state transition from %s to %s\n",
  706. __func__, state_to_str[old_state],
  707. state_to_str[cdc_clsh_d->state]);
  708. break;
  709. case AQT_CLSH_EVENT_POST_PA:
  710. old_state = cdc_clsh_d->state;
  711. new_state = old_state & (~req_state);
  712. if (new_state < NUM_CLSH_STATES) {
  713. if (!aqt_clsh_is_state_valid(old_state)) {
  714. dev_err(component->dev,
  715. "%s:Invalid old state:%s\n",
  716. __func__, state_to_str[old_state]);
  717. return;
  718. }
  719. if (new_state == old_state) {
  720. dev_err(component->dev,
  721. "%s: Class-H already in requested state: %s\n",
  722. __func__,state_to_str[new_state]);
  723. return;
  724. }
  725. (*clsh_state_fp[old_state]) (component, cdc_clsh_d,
  726. req_state, CLSH_REQ_DISABLE,
  727. int_mode);
  728. cdc_clsh_d->state = new_state;
  729. aqt_clsh_set_int_mode(cdc_clsh_d, req_state, CLS_NONE);
  730. dev_dbg(component->dev, "%s: ClassH state transition from %s to %s\n",
  731. __func__, state_to_str[old_state],
  732. state_to_str[cdc_clsh_d->state]);
  733. }
  734. break;
  735. };
  736. }
  737. EXPORT_SYMBOL(aqt_clsh_fsm);
  738. /*
  739. * Function: aqt_clsh_get_clsh_state
  740. * Params: clsh
  741. * Description:
  742. * This function returns the state of the class H controller
  743. */
  744. int aqt_clsh_get_clsh_state(struct aqt_clsh_cdc_data *clsh)
  745. {
  746. return clsh->state;
  747. }
  748. EXPORT_SYMBOL(aqt_clsh_get_clsh_state);
  749. /*
  750. * Function: aqt_clsh_init
  751. * Params: clsh
  752. * Description:
  753. * This function initializes the class H controller
  754. */
  755. void aqt_clsh_init(struct aqt_clsh_cdc_data *clsh)
  756. {
  757. int i;
  758. clsh->state = AQT_CLSH_STATE_IDLE;
  759. for (i = 0; i < NUM_CLSH_STATES; i++)
  760. clsh_state_fp[i] = aqt_clsh_state_err;
  761. clsh_state_fp[AQT_CLSH_STATE_HPHL] = aqt_clsh_state_hph_l;
  762. clsh_state_fp[AQT_CLSH_STATE_HPHR] = aqt_clsh_state_hph_r;
  763. clsh_state_fp[AQT_CLSH_STATE_HPH_ST] = aqt_clsh_state_hph_st;
  764. /* Set interpolator modes to NONE */
  765. aqt_clsh_set_int_mode(clsh, AQT_CLSH_STATE_HPHL, CLS_NONE);
  766. aqt_clsh_set_int_mode(clsh, AQT_CLSH_STATE_HPHR, CLS_NONE);
  767. clsh->flyback_users = 0;
  768. clsh->buck_users = 0;
  769. clsh->clsh_users = 0;
  770. }
  771. EXPORT_SYMBOL(aqt_clsh_init);