wcd934x-dsd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/delay.h>
  6. #include <sound/tlv.h>
  7. #include <sound/control.h>
  8. #include <asoc/wcd934x_registers.h>
  9. #include "wcd934x-dsd.h"
  10. #define DSD_VOLUME_MAX_0dB 0
  11. #define DSD_VOLUME_MIN_M110dB -110
  12. #define DSD_VOLUME_RANGE_CHECK(x) ((x >= DSD_VOLUME_MIN_M110dB) &&\
  13. (x <= DSD_VOLUME_MAX_0dB))
  14. #define DSD_VOLUME_STEPS 3
  15. #define DSD_VOLUME_UPDATE_DELAY_MS 30
  16. #define DSD_VOLUME_USLEEP_MARGIN_US 100
  17. #define DSD_VOLUME_STEP_DELAY_US ((1000 * DSD_VOLUME_UPDATE_DELAY_MS) / \
  18. (2 * DSD_VOLUME_STEPS))
  19. #define TAVIL_VERSION_1_0 0
  20. #define TAVIL_VERSION_1_1 1
  21. static const DECLARE_TLV_DB_MINMAX(tavil_dsd_db_scale, DSD_VOLUME_MIN_M110dB,
  22. DSD_VOLUME_MAX_0dB);
  23. static const char *const dsd_if_text[] = {
  24. "ZERO", "RX0", "RX1", "RX2", "RX3", "RX4", "RX5", "RX6", "RX7",
  25. "DSD_DATA_PAD"
  26. };
  27. static const char * const dsd_filt0_mux_text[] = {
  28. "ZERO", "DSD_L IF MUX",
  29. };
  30. static const char * const dsd_filt1_mux_text[] = {
  31. "ZERO", "DSD_R IF MUX",
  32. };
  33. static const struct soc_enum dsd_filt0_mux_enum =
  34. SOC_ENUM_SINGLE(WCD934X_CDC_DSD0_PATH_CTL, 0,
  35. ARRAY_SIZE(dsd_filt0_mux_text), dsd_filt0_mux_text);
  36. static const struct soc_enum dsd_filt1_mux_enum =
  37. SOC_ENUM_SINGLE(WCD934X_CDC_DSD1_PATH_CTL, 0,
  38. ARRAY_SIZE(dsd_filt1_mux_text), dsd_filt1_mux_text);
  39. static SOC_ENUM_SINGLE_DECL(dsd_l_if_enum, WCD934X_CDC_DSD0_CFG0,
  40. 2, dsd_if_text);
  41. static SOC_ENUM_SINGLE_DECL(dsd_r_if_enum, WCD934X_CDC_DSD1_CFG0,
  42. 2, dsd_if_text);
  43. static const struct snd_kcontrol_new dsd_filt0_mux =
  44. SOC_DAPM_ENUM("DSD Filt0 Mux", dsd_filt0_mux_enum);
  45. static const struct snd_kcontrol_new dsd_filt1_mux =
  46. SOC_DAPM_ENUM("DSD Filt1 Mux", dsd_filt1_mux_enum);
  47. static const struct snd_kcontrol_new dsd_l_if_mux =
  48. SOC_DAPM_ENUM("DSD Left If Mux", dsd_l_if_enum);
  49. static const struct snd_kcontrol_new dsd_r_if_mux =
  50. SOC_DAPM_ENUM("DSD Right If Mux", dsd_r_if_enum);
  51. static const struct snd_soc_dapm_route tavil_dsd_audio_map[] = {
  52. {"DSD_L IF MUX", "RX0", "CDC_IF RX0 MUX"},
  53. {"DSD_L IF MUX", "RX1", "CDC_IF RX1 MUX"},
  54. {"DSD_L IF MUX", "RX2", "CDC_IF RX2 MUX"},
  55. {"DSD_L IF MUX", "RX3", "CDC_IF RX3 MUX"},
  56. {"DSD_L IF MUX", "RX4", "CDC_IF RX4 MUX"},
  57. {"DSD_L IF MUX", "RX5", "CDC_IF RX5 MUX"},
  58. {"DSD_L IF MUX", "RX6", "CDC_IF RX6 MUX"},
  59. {"DSD_L IF MUX", "RX7", "CDC_IF RX7 MUX"},
  60. {"DSD_FILTER_0", NULL, "DSD_L IF MUX"},
  61. {"DSD_FILTER_0", NULL, "RX INT1 NATIVE SUPPLY"},
  62. {"RX INT1 MIX3", "DSD HPHL Switch", "DSD_FILTER_0"},
  63. {"DSD_R IF MUX", "RX0", "CDC_IF RX0 MUX"},
  64. {"DSD_R IF MUX", "RX1", "CDC_IF RX1 MUX"},
  65. {"DSD_R IF MUX", "RX2", "CDC_IF RX2 MUX"},
  66. {"DSD_R IF MUX", "RX3", "CDC_IF RX3 MUX"},
  67. {"DSD_R IF MUX", "RX4", "CDC_IF RX4 MUX"},
  68. {"DSD_R IF MUX", "RX5", "CDC_IF RX5 MUX"},
  69. {"DSD_R IF MUX", "RX6", "CDC_IF RX6 MUX"},
  70. {"DSD_R IF MUX", "RX7", "CDC_IF RX7 MUX"},
  71. {"DSD_FILTER_1", NULL, "DSD_R IF MUX"},
  72. {"DSD_FILTER_1", NULL, "RX INT2 NATIVE SUPPLY"},
  73. {"RX INT2 MIX3", "DSD HPHR Switch", "DSD_FILTER_1"},
  74. {"DSD_FILTER_0", NULL, "RX INT3 NATIVE SUPPLY"},
  75. {"RX INT3 MIX3", "DSD LO1 Switch", "DSD_FILTER_0"},
  76. {"DSD_FILTER_1", NULL, "RX INT4 NATIVE SUPPLY"},
  77. {"RX INT4 MIX3", "DSD LO2 Switch", "DSD_FILTER_1"},
  78. };
  79. static bool is_valid_dsd_interpolator(int interp_num)
  80. {
  81. if ((interp_num == INTERP_HPHL) || (interp_num == INTERP_HPHR) ||
  82. (interp_num == INTERP_LO1) || (interp_num == INTERP_LO2))
  83. return true;
  84. return false;
  85. }
  86. /**
  87. * tavil_dsd_set_mixer_value - Set DSD HPH/LO mixer value
  88. *
  89. * @dsd_conf: pointer to dsd config
  90. * @interp_num: Interpolator number (HPHL/R, LO1/2)
  91. * @sw_value: Mixer switch value
  92. *
  93. * Returns 0 on success or -EINVAL on failure
  94. */
  95. int tavil_dsd_set_mixer_value(struct tavil_dsd_config *dsd_conf,
  96. int interp_num, int sw_value)
  97. {
  98. if (!dsd_conf)
  99. return -EINVAL;
  100. if (!is_valid_dsd_interpolator(interp_num))
  101. return -EINVAL;
  102. dsd_conf->dsd_interp_mixer[interp_num] = !!sw_value;
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(tavil_dsd_set_mixer_value);
  106. /**
  107. * tavil_dsd_get_current_mixer_value - Get DSD HPH/LO mixer value
  108. *
  109. * @dsd_conf: pointer to dsd config
  110. * @interp_num: Interpolator number (HPHL/R, LO1/2)
  111. *
  112. * Returns current mixer val for success or -EINVAL for failure
  113. */
  114. int tavil_dsd_get_current_mixer_value(struct tavil_dsd_config *dsd_conf,
  115. int interp_num)
  116. {
  117. if (!dsd_conf)
  118. return -EINVAL;
  119. if (!is_valid_dsd_interpolator(interp_num))
  120. return -EINVAL;
  121. return dsd_conf->dsd_interp_mixer[interp_num];
  122. }
  123. EXPORT_SYMBOL(tavil_dsd_get_current_mixer_value);
  124. /**
  125. * tavil_dsd_set_out_select - DSD0/1 out select to HPH or LO
  126. *
  127. * @dsd_conf: pointer to dsd config
  128. * @interp_num: Interpolator number (HPHL/R, LO1/2)
  129. *
  130. * Returns 0 for success or -EINVAL for failure
  131. */
  132. int tavil_dsd_set_out_select(struct tavil_dsd_config *dsd_conf,
  133. int interp_num)
  134. {
  135. unsigned int reg, val;
  136. struct snd_soc_codec *codec;
  137. if (!dsd_conf || !dsd_conf->codec)
  138. return -EINVAL;
  139. codec = dsd_conf->codec;
  140. if (!is_valid_dsd_interpolator(interp_num)) {
  141. dev_err(codec->dev, "%s: Invalid Interpolator: %d for DSD\n",
  142. __func__, interp_num);
  143. return -EINVAL;
  144. }
  145. switch (interp_num) {
  146. case INTERP_HPHL:
  147. reg = WCD934X_CDC_DSD0_CFG0;
  148. val = 0x00;
  149. break;
  150. case INTERP_HPHR:
  151. reg = WCD934X_CDC_DSD1_CFG0;
  152. val = 0x00;
  153. break;
  154. case INTERP_LO1:
  155. reg = WCD934X_CDC_DSD0_CFG0;
  156. val = 0x02;
  157. break;
  158. case INTERP_LO2:
  159. reg = WCD934X_CDC_DSD1_CFG0;
  160. val = 0x02;
  161. break;
  162. default:
  163. return -EINVAL;
  164. }
  165. snd_soc_update_bits(codec, reg, 0x02, val);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(tavil_dsd_set_out_select);
  169. /**
  170. * tavil_dsd_reset - Reset DSD block
  171. *
  172. * @dsd_conf: pointer to dsd config
  173. *
  174. */
  175. void tavil_dsd_reset(struct tavil_dsd_config *dsd_conf)
  176. {
  177. if (!dsd_conf || !dsd_conf->codec)
  178. return;
  179. snd_soc_update_bits(dsd_conf->codec, WCD934X_CDC_DSD0_PATH_CTL,
  180. 0x02, 0x02);
  181. snd_soc_update_bits(dsd_conf->codec, WCD934X_CDC_DSD0_PATH_CTL,
  182. 0x01, 0x00);
  183. snd_soc_update_bits(dsd_conf->codec, WCD934X_CDC_DSD1_PATH_CTL,
  184. 0x02, 0x02);
  185. snd_soc_update_bits(dsd_conf->codec, WCD934X_CDC_DSD1_PATH_CTL,
  186. 0x01, 0x00);
  187. }
  188. EXPORT_SYMBOL(tavil_dsd_reset);
  189. /**
  190. * tavil_dsd_set_interp_rate - Set interpolator rate for DSD
  191. *
  192. * @dsd_conf: pointer to dsd config
  193. * @rx_port: RX port number
  194. * @sample_rate: Sample rate of the RX interpolator
  195. * @sample_rate_val: Interpolator rate value
  196. */
  197. void tavil_dsd_set_interp_rate(struct tavil_dsd_config *dsd_conf, u16 rx_port,
  198. u32 sample_rate, u8 sample_rate_val)
  199. {
  200. u8 dsd_inp_sel;
  201. u8 dsd0_inp, dsd1_inp;
  202. u8 val0, val1;
  203. u8 dsd0_out_sel, dsd1_out_sel;
  204. u16 int_fs_reg, interp_num = 0;
  205. struct snd_soc_codec *codec;
  206. if (!dsd_conf || !dsd_conf->codec)
  207. return;
  208. codec = dsd_conf->codec;
  209. dsd_inp_sel = DSD_INP_SEL_RX0 + rx_port - WCD934X_RX_PORT_START_NUMBER;
  210. val0 = snd_soc_read(codec, WCD934X_CDC_DSD0_CFG0);
  211. val1 = snd_soc_read(codec, WCD934X_CDC_DSD1_CFG0);
  212. dsd0_inp = (val0 & 0x3C) >> 2;
  213. dsd1_inp = (val1 & 0x3C) >> 2;
  214. dsd0_out_sel = (val0 & 0x02) >> 1;
  215. dsd1_out_sel = (val1 & 0x02) >> 1;
  216. /* Set HPHL or LO1 interp rate based on out select */
  217. if (dsd_inp_sel == dsd0_inp) {
  218. interp_num = dsd0_out_sel ? INTERP_LO1 : INTERP_HPHL;
  219. dsd_conf->base_sample_rate[DSD0] = sample_rate;
  220. }
  221. /* Set HPHR or LO2 interp rate based on out select */
  222. if (dsd_inp_sel == dsd1_inp) {
  223. interp_num = dsd1_out_sel ? INTERP_LO2 : INTERP_HPHR;
  224. dsd_conf->base_sample_rate[DSD1] = sample_rate;
  225. }
  226. if (interp_num) {
  227. int_fs_reg = WCD934X_CDC_RX0_RX_PATH_CTL + 20 * interp_num;
  228. if ((snd_soc_read(codec, int_fs_reg) & 0x0f) < 0x09) {
  229. dev_dbg(codec->dev, "%s: Set Interp %d to sample_rate val 0x%x\n",
  230. __func__, interp_num, sample_rate_val);
  231. snd_soc_update_bits(codec, int_fs_reg, 0x0F,
  232. sample_rate_val);
  233. }
  234. }
  235. }
  236. EXPORT_SYMBOL(tavil_dsd_set_interp_rate);
  237. static int tavil_set_dsd_mode(struct snd_soc_codec *codec, int dsd_num,
  238. u8 *pcm_rate_val)
  239. {
  240. unsigned int dsd_out_sel_reg;
  241. u8 dsd_mode;
  242. u32 sample_rate;
  243. struct tavil_dsd_config *dsd_conf = tavil_get_dsd_config(codec);
  244. if (!dsd_conf)
  245. return -EINVAL;
  246. if ((dsd_num < 0) || (dsd_num > 1))
  247. return -EINVAL;
  248. sample_rate = dsd_conf->base_sample_rate[dsd_num];
  249. dsd_out_sel_reg = WCD934X_CDC_DSD0_CFG0 + dsd_num * 16;
  250. switch (sample_rate) {
  251. case 176400:
  252. dsd_mode = 0; /* DSD_64 */
  253. *pcm_rate_val = 0xb;
  254. break;
  255. case 352800:
  256. dsd_mode = 1; /* DSD_128 */
  257. *pcm_rate_val = 0xc;
  258. break;
  259. default:
  260. dev_err(codec->dev, "%s: Invalid DSD rate: %d\n",
  261. __func__, sample_rate);
  262. return -EINVAL;
  263. }
  264. snd_soc_update_bits(codec, dsd_out_sel_reg, 0x01, dsd_mode);
  265. return 0;
  266. }
  267. static void tavil_dsd_data_pull(struct snd_soc_codec *codec, int dsd_num,
  268. u8 pcm_rate_val, bool enable)
  269. {
  270. u8 clk_en, mute_en;
  271. u8 dsd_inp_sel;
  272. if (enable) {
  273. clk_en = 0x20;
  274. mute_en = 0x10;
  275. } else {
  276. clk_en = 0x00;
  277. mute_en = 0x00;
  278. }
  279. if (dsd_num & 0x01) {
  280. snd_soc_update_bits(codec, WCD934X_CDC_RX7_RX_PATH_MIX_CTL,
  281. 0x20, clk_en);
  282. dsd_inp_sel = (snd_soc_read(codec, WCD934X_CDC_DSD0_CFG0) &
  283. 0x3C) >> 2;
  284. dsd_inp_sel = (enable) ? dsd_inp_sel : 0;
  285. if (dsd_inp_sel < 9) {
  286. snd_soc_update_bits(codec,
  287. WCD934X_CDC_RX_INP_MUX_RX_INT7_CFG1,
  288. 0x0F, dsd_inp_sel);
  289. snd_soc_update_bits(codec,
  290. WCD934X_CDC_RX7_RX_PATH_MIX_CTL,
  291. 0x0F, pcm_rate_val);
  292. snd_soc_update_bits(codec,
  293. WCD934X_CDC_RX7_RX_PATH_MIX_CTL,
  294. 0x10, mute_en);
  295. }
  296. }
  297. if (dsd_num & 0x02) {
  298. snd_soc_update_bits(codec, WCD934X_CDC_RX8_RX_PATH_MIX_CTL,
  299. 0x20, clk_en);
  300. dsd_inp_sel = (snd_soc_read(codec, WCD934X_CDC_DSD1_CFG0) &
  301. 0x3C) >> 2;
  302. dsd_inp_sel = (enable) ? dsd_inp_sel : 0;
  303. if (dsd_inp_sel < 9) {
  304. snd_soc_update_bits(codec,
  305. WCD934X_CDC_RX_INP_MUX_RX_INT8_CFG1,
  306. 0x0F, dsd_inp_sel);
  307. snd_soc_update_bits(codec,
  308. WCD934X_CDC_RX8_RX_PATH_MIX_CTL,
  309. 0x0F, pcm_rate_val);
  310. snd_soc_update_bits(codec,
  311. WCD934X_CDC_RX8_RX_PATH_MIX_CTL,
  312. 0x10, mute_en);
  313. }
  314. }
  315. }
  316. static void tavil_dsd_update_volume(struct tavil_dsd_config *dsd_conf)
  317. {
  318. snd_soc_update_bits(dsd_conf->codec, WCD934X_CDC_TOP_TOP_CFG0,
  319. 0x01, 0x01);
  320. snd_soc_update_bits(dsd_conf->codec, WCD934X_CDC_TOP_TOP_CFG0,
  321. 0x01, 0x00);
  322. }
  323. static int tavil_enable_dsd(struct snd_soc_dapm_widget *w,
  324. struct snd_kcontrol *kcontrol, int event)
  325. {
  326. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  327. struct tavil_dsd_config *dsd_conf = tavil_get_dsd_config(codec);
  328. int rc, clk_users;
  329. int interp_idx;
  330. u8 pcm_rate_val;
  331. if (!dsd_conf) {
  332. dev_err(codec->dev, "%s: null dsd_config pointer\n", __func__);
  333. return -EINVAL;
  334. }
  335. dev_dbg(codec->dev, "%s: DSD%d, event: %d\n", __func__,
  336. w->shift, event);
  337. if (w->shift == DSD0) {
  338. /* Read out select */
  339. if (snd_soc_read(codec, WCD934X_CDC_DSD0_CFG0) & 0x02)
  340. interp_idx = INTERP_LO1;
  341. else
  342. interp_idx = INTERP_HPHL;
  343. } else if (w->shift == DSD1) {
  344. /* Read out select */
  345. if (snd_soc_read(codec, WCD934X_CDC_DSD1_CFG0) & 0x02)
  346. interp_idx = INTERP_LO2;
  347. else
  348. interp_idx = INTERP_HPHR;
  349. } else {
  350. dev_err(codec->dev, "%s: Unsupported DSD:%d\n",
  351. __func__, w->shift);
  352. return -EINVAL;
  353. }
  354. switch (event) {
  355. case SND_SOC_DAPM_PRE_PMU:
  356. clk_users = tavil_codec_enable_interp_clk(codec, event,
  357. interp_idx);
  358. rc = tavil_set_dsd_mode(codec, w->shift, &pcm_rate_val);
  359. if (rc)
  360. return rc;
  361. tavil_dsd_data_pull(codec, (1 << w->shift), pcm_rate_val,
  362. true);
  363. snd_soc_update_bits(codec,
  364. WCD934X_CDC_CLK_RST_CTRL_DSD_CONTROL, 0x01,
  365. 0x01);
  366. if (w->shift == DSD0) {
  367. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_PATH_CTL,
  368. 0x02, 0x02);
  369. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_PATH_CTL,
  370. 0x02, 0x00);
  371. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_PATH_CTL,
  372. 0x01, 0x01);
  373. /* Apply Gain */
  374. snd_soc_write(codec, WCD934X_CDC_DSD0_CFG1,
  375. dsd_conf->volume[DSD0]);
  376. if (dsd_conf->version == TAVIL_VERSION_1_1)
  377. tavil_dsd_update_volume(dsd_conf);
  378. } else if (w->shift == DSD1) {
  379. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_PATH_CTL,
  380. 0x02, 0x02);
  381. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_PATH_CTL,
  382. 0x02, 0x00);
  383. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_PATH_CTL,
  384. 0x01, 0x01);
  385. /* Apply Gain */
  386. snd_soc_write(codec, WCD934X_CDC_DSD1_CFG1,
  387. dsd_conf->volume[DSD1]);
  388. if (dsd_conf->version == TAVIL_VERSION_1_1)
  389. tavil_dsd_update_volume(dsd_conf);
  390. }
  391. /* 10msec sleep required after DSD clock is set */
  392. usleep_range(10000, 10100);
  393. if (clk_users > 1) {
  394. snd_soc_update_bits(codec, WCD934X_ANA_RX_SUPPLIES,
  395. 0x02, 0x02);
  396. if (w->shift == DSD0)
  397. snd_soc_update_bits(codec,
  398. WCD934X_CDC_DSD0_CFG2,
  399. 0x04, 0x00);
  400. if (w->shift == DSD1)
  401. snd_soc_update_bits(codec,
  402. WCD934X_CDC_DSD1_CFG2,
  403. 0x04, 0x00);
  404. }
  405. break;
  406. case SND_SOC_DAPM_POST_PMD:
  407. if (w->shift == DSD0) {
  408. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_CFG2,
  409. 0x04, 0x04);
  410. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_PATH_CTL,
  411. 0x01, 0x00);
  412. } else if (w->shift == DSD1) {
  413. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_CFG2,
  414. 0x04, 0x04);
  415. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_PATH_CTL,
  416. 0x01, 0x00);
  417. }
  418. tavil_codec_enable_interp_clk(codec, event, interp_idx);
  419. if (!(snd_soc_read(codec, WCD934X_CDC_DSD0_PATH_CTL) & 0x01) &&
  420. !(snd_soc_read(codec, WCD934X_CDC_DSD1_PATH_CTL) & 0x01)) {
  421. snd_soc_update_bits(codec,
  422. WCD934X_CDC_CLK_RST_CTRL_DSD_CONTROL,
  423. 0x01, 0x00);
  424. tavil_dsd_data_pull(codec, 0x03, 0x04, false);
  425. tavil_dsd_reset(dsd_conf);
  426. }
  427. break;
  428. }
  429. return 0;
  430. }
  431. static int tavil_dsd_vol_info(struct snd_kcontrol *kcontrol,
  432. struct snd_ctl_elem_info *uinfo)
  433. {
  434. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  435. uinfo->count = 2;
  436. uinfo->value.integer.min = DSD_VOLUME_MIN_M110dB;
  437. uinfo->value.integer.max = DSD_VOLUME_MAX_0dB;
  438. return 0;
  439. }
  440. static int tavil_dsd_vol_put(struct snd_kcontrol *kcontrol,
  441. struct snd_ctl_elem_value *ucontrol)
  442. {
  443. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  444. struct tavil_dsd_config *dsd_conf = tavil_get_dsd_config(codec);
  445. int nv[DSD_MAX], cv[DSD_MAX];
  446. int step_size, nv1;
  447. int i, dsd_idx;
  448. if (!dsd_conf)
  449. return 0;
  450. mutex_lock(&dsd_conf->vol_mutex);
  451. for (dsd_idx = DSD0; dsd_idx < DSD_MAX; dsd_idx++) {
  452. cv[dsd_idx] = dsd_conf->volume[dsd_idx];
  453. nv[dsd_idx] = ucontrol->value.integer.value[dsd_idx];
  454. }
  455. if ((!DSD_VOLUME_RANGE_CHECK(nv[DSD0])) ||
  456. (!DSD_VOLUME_RANGE_CHECK(nv[DSD1])))
  457. goto done;
  458. for (dsd_idx = DSD0; dsd_idx < DSD_MAX; dsd_idx++) {
  459. if (cv[dsd_idx] == nv[dsd_idx])
  460. continue;
  461. dev_dbg(codec->dev, "%s: DSD%d cur.vol: %d, new vol: %d\n",
  462. __func__, dsd_idx, cv[dsd_idx], nv[dsd_idx]);
  463. step_size = (nv[dsd_idx] - cv[dsd_idx]) /
  464. DSD_VOLUME_STEPS;
  465. nv1 = cv[dsd_idx];
  466. for (i = 0; i < DSD_VOLUME_STEPS; i++) {
  467. nv1 += step_size;
  468. snd_soc_write(codec,
  469. WCD934X_CDC_DSD0_CFG1 + 16 * dsd_idx,
  470. nv1);
  471. if (dsd_conf->version == TAVIL_VERSION_1_1)
  472. tavil_dsd_update_volume(dsd_conf);
  473. /* sleep required after each volume step */
  474. usleep_range(DSD_VOLUME_STEP_DELAY_US,
  475. (DSD_VOLUME_STEP_DELAY_US +
  476. DSD_VOLUME_USLEEP_MARGIN_US));
  477. }
  478. if (nv1 != nv[dsd_idx]) {
  479. snd_soc_write(codec,
  480. WCD934X_CDC_DSD0_CFG1 + 16 * dsd_idx,
  481. nv[dsd_idx]);
  482. if (dsd_conf->version == TAVIL_VERSION_1_1)
  483. tavil_dsd_update_volume(dsd_conf);
  484. }
  485. dsd_conf->volume[dsd_idx] = nv[dsd_idx];
  486. }
  487. done:
  488. mutex_unlock(&dsd_conf->vol_mutex);
  489. return 0;
  490. }
  491. static int tavil_dsd_vol_get(struct snd_kcontrol *kcontrol,
  492. struct snd_ctl_elem_value *ucontrol)
  493. {
  494. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  495. struct tavil_dsd_config *dsd_conf = tavil_get_dsd_config(codec);
  496. if (dsd_conf) {
  497. ucontrol->value.integer.value[0] = dsd_conf->volume[DSD0];
  498. ucontrol->value.integer.value[1] = dsd_conf->volume[DSD1];
  499. }
  500. return 0;
  501. }
  502. static const struct snd_kcontrol_new tavil_dsd_vol_controls[] = {
  503. {
  504. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  505. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  506. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  507. .name = "DSD Volume",
  508. .info = tavil_dsd_vol_info,
  509. .get = tavil_dsd_vol_get,
  510. .put = tavil_dsd_vol_put,
  511. .tlv = { .p = tavil_dsd_db_scale },
  512. },
  513. };
  514. static const struct snd_soc_dapm_widget tavil_dsd_widgets[] = {
  515. SND_SOC_DAPM_MUX("DSD_L IF MUX", SND_SOC_NOPM, 0, 0, &dsd_l_if_mux),
  516. SND_SOC_DAPM_MUX_E("DSD_FILTER_0", SND_SOC_NOPM, 0, 0, &dsd_filt0_mux,
  517. tavil_enable_dsd,
  518. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  519. SND_SOC_DAPM_MUX("DSD_R IF MUX", SND_SOC_NOPM, 0, 0, &dsd_r_if_mux),
  520. SND_SOC_DAPM_MUX_E("DSD_FILTER_1", SND_SOC_NOPM, 1, 0, &dsd_filt1_mux,
  521. tavil_enable_dsd,
  522. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  523. };
  524. /**
  525. * tavil_dsd_post_ssr_init - DSD intialization after subsystem restart
  526. *
  527. * @codec: pointer to snd_soc_codec
  528. *
  529. * Returns 0 on success or error on failure
  530. */
  531. int tavil_dsd_post_ssr_init(struct tavil_dsd_config *dsd_conf)
  532. {
  533. struct snd_soc_codec *codec;
  534. if (!dsd_conf || !dsd_conf->codec)
  535. return -EINVAL;
  536. codec = dsd_conf->codec;
  537. /* Disable DSD Interrupts */
  538. snd_soc_update_bits(codec, WCD934X_INTR_CODEC_MISC_MASK, 0x08, 0x08);
  539. /* DSD registers init */
  540. if (dsd_conf->version == TAVIL_VERSION_1_0) {
  541. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_CFG2, 0x02, 0x00);
  542. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_CFG2, 0x02, 0x00);
  543. }
  544. /* DSD0: Mute EN */
  545. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_CFG2, 0x04, 0x04);
  546. /* DSD1: Mute EN */
  547. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_CFG2, 0x04, 0x04);
  548. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD0_DEBUG_CFG3, 0x10,
  549. 0x10);
  550. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD1_DEBUG_CFG3, 0x10,
  551. 0x10);
  552. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD0_DEBUG_CFG0, 0x0E,
  553. 0x0A);
  554. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD1_DEBUG_CFG0, 0x0E,
  555. 0x0A);
  556. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD0_DEBUG_CFG1, 0x07,
  557. 0x04);
  558. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD1_DEBUG_CFG1, 0x07,
  559. 0x04);
  560. /* Enable DSD Interrupts */
  561. snd_soc_update_bits(codec, WCD934X_INTR_CODEC_MISC_MASK, 0x08, 0x00);
  562. return 0;
  563. }
  564. EXPORT_SYMBOL(tavil_dsd_post_ssr_init);
  565. /**
  566. * tavil_dsd_init - DSD intialization
  567. *
  568. * @codec: pointer to snd_soc_codec
  569. *
  570. * Returns pointer to tavil_dsd_config for success or NULL for failure
  571. */
  572. struct tavil_dsd_config *tavil_dsd_init(struct snd_soc_codec *codec)
  573. {
  574. struct snd_soc_dapm_context *dapm;
  575. struct tavil_dsd_config *dsd_conf;
  576. u8 val;
  577. if (!codec)
  578. return NULL;
  579. dapm = snd_soc_codec_get_dapm(codec);
  580. /* Read efuse register to check if DSD is supported */
  581. val = snd_soc_read(codec, WCD934X_CHIP_TIER_CTRL_EFUSE_VAL_OUT14);
  582. if (val & 0x80) {
  583. dev_info(codec->dev, "%s: DSD unsupported for this codec version\n",
  584. __func__);
  585. return NULL;
  586. }
  587. dsd_conf = devm_kzalloc(codec->dev, sizeof(struct tavil_dsd_config),
  588. GFP_KERNEL);
  589. if (!dsd_conf)
  590. return NULL;
  591. dsd_conf->codec = codec;
  592. /* Read version */
  593. dsd_conf->version = snd_soc_read(codec,
  594. WCD934X_CHIP_TIER_CTRL_CHIP_ID_BYTE0);
  595. /* DSD registers init */
  596. if (dsd_conf->version == TAVIL_VERSION_1_0) {
  597. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_CFG2, 0x02, 0x00);
  598. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_CFG2, 0x02, 0x00);
  599. }
  600. /* DSD0: Mute EN */
  601. snd_soc_update_bits(codec, WCD934X_CDC_DSD0_CFG2, 0x04, 0x04);
  602. /* DSD1: Mute EN */
  603. snd_soc_update_bits(codec, WCD934X_CDC_DSD1_CFG2, 0x04, 0x04);
  604. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD0_DEBUG_CFG3, 0x10,
  605. 0x10);
  606. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD1_DEBUG_CFG3, 0x10,
  607. 0x10);
  608. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD0_DEBUG_CFG0, 0x0E,
  609. 0x0A);
  610. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD1_DEBUG_CFG0, 0x0E,
  611. 0x0A);
  612. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD0_DEBUG_CFG1, 0x07,
  613. 0x04);
  614. snd_soc_update_bits(codec, WCD934X_CDC_DEBUG_DSD1_DEBUG_CFG1, 0x07,
  615. 0x04);
  616. snd_soc_dapm_new_controls(dapm, tavil_dsd_widgets,
  617. ARRAY_SIZE(tavil_dsd_widgets));
  618. snd_soc_dapm_add_routes(dapm, tavil_dsd_audio_map,
  619. ARRAY_SIZE(tavil_dsd_audio_map));
  620. mutex_init(&dsd_conf->vol_mutex);
  621. dsd_conf->volume[DSD0] = DSD_VOLUME_MAX_0dB;
  622. dsd_conf->volume[DSD1] = DSD_VOLUME_MAX_0dB;
  623. snd_soc_add_codec_controls(codec, tavil_dsd_vol_controls,
  624. ARRAY_SIZE(tavil_dsd_vol_controls));
  625. /* Enable DSD Interrupts */
  626. snd_soc_update_bits(codec, WCD934X_INTR_CODEC_MISC_MASK, 0x08, 0x00);
  627. return dsd_conf;
  628. }
  629. EXPORT_SYMBOL(tavil_dsd_init);
  630. /**
  631. * tavil_dsd_deinit - DSD de-intialization
  632. *
  633. * @dsd_conf: pointer to tavil_dsd_config
  634. */
  635. void tavil_dsd_deinit(struct tavil_dsd_config *dsd_conf)
  636. {
  637. struct snd_soc_codec *codec;
  638. if (!dsd_conf)
  639. return;
  640. codec = dsd_conf->codec;
  641. mutex_destroy(&dsd_conf->vol_mutex);
  642. /* Disable DSD Interrupts */
  643. snd_soc_update_bits(codec, WCD934X_INTR_CODEC_MISC_MASK, 0x08, 0x08);
  644. devm_kfree(codec->dev, dsd_conf);
  645. }
  646. EXPORT_SYMBOL(tavil_dsd_deinit);