wcd934x-dsd.c 21 KB

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