tegra186_dspk.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // tegra186_dspk.c - Tegra186 DSPK driver
  4. //
  5. // Copyright (c) 2020 NVIDIA CORPORATION. All rights reserved.
  6. #include <linux/clk.h>
  7. #include <linux/device.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/regmap.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm_params.h>
  16. #include <sound/soc.h>
  17. #include "tegra186_dspk.h"
  18. #include "tegra_cif.h"
  19. static const struct reg_default tegra186_dspk_reg_defaults[] = {
  20. { TEGRA186_DSPK_RX_INT_MASK, 0x00000007 },
  21. { TEGRA186_DSPK_RX_CIF_CTRL, 0x00007700 },
  22. { TEGRA186_DSPK_CG, 0x00000001 },
  23. { TEGRA186_DSPK_CORE_CTRL, 0x00000310 },
  24. { TEGRA186_DSPK_CODEC_CTRL, 0x03000000 },
  25. };
  26. static int tegra186_dspk_get_fifo_th(struct snd_kcontrol *kcontrol,
  27. struct snd_ctl_elem_value *ucontrol)
  28. {
  29. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  30. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  31. ucontrol->value.integer.value[0] = dspk->rx_fifo_th;
  32. return 0;
  33. }
  34. static int tegra186_dspk_put_fifo_th(struct snd_kcontrol *kcontrol,
  35. struct snd_ctl_elem_value *ucontrol)
  36. {
  37. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  38. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  39. int value = ucontrol->value.integer.value[0];
  40. if (value == dspk->rx_fifo_th)
  41. return 0;
  42. dspk->rx_fifo_th = value;
  43. return 1;
  44. }
  45. static int tegra186_dspk_get_osr_val(struct snd_kcontrol *kcontrol,
  46. struct snd_ctl_elem_value *ucontrol)
  47. {
  48. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  49. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  50. ucontrol->value.enumerated.item[0] = dspk->osr_val;
  51. return 0;
  52. }
  53. static int tegra186_dspk_put_osr_val(struct snd_kcontrol *kcontrol,
  54. struct snd_ctl_elem_value *ucontrol)
  55. {
  56. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  57. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  58. unsigned int value = ucontrol->value.enumerated.item[0];
  59. if (value == dspk->osr_val)
  60. return 0;
  61. dspk->osr_val = value;
  62. return 1;
  63. }
  64. static int tegra186_dspk_get_pol_sel(struct snd_kcontrol *kcontrol,
  65. struct snd_ctl_elem_value *ucontrol)
  66. {
  67. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  68. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  69. ucontrol->value.enumerated.item[0] = dspk->lrsel;
  70. return 0;
  71. }
  72. static int tegra186_dspk_put_pol_sel(struct snd_kcontrol *kcontrol,
  73. struct snd_ctl_elem_value *ucontrol)
  74. {
  75. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  76. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  77. unsigned int value = ucontrol->value.enumerated.item[0];
  78. if (value == dspk->lrsel)
  79. return 0;
  80. dspk->lrsel = value;
  81. return 1;
  82. }
  83. static int tegra186_dspk_get_ch_sel(struct snd_kcontrol *kcontrol,
  84. struct snd_ctl_elem_value *ucontrol)
  85. {
  86. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  87. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  88. ucontrol->value.enumerated.item[0] = dspk->ch_sel;
  89. return 0;
  90. }
  91. static int tegra186_dspk_put_ch_sel(struct snd_kcontrol *kcontrol,
  92. struct snd_ctl_elem_value *ucontrol)
  93. {
  94. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  95. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  96. unsigned int value = ucontrol->value.enumerated.item[0];
  97. if (value == dspk->ch_sel)
  98. return 0;
  99. dspk->ch_sel = value;
  100. return 1;
  101. }
  102. static int tegra186_dspk_get_mono_to_stereo(struct snd_kcontrol *kcontrol,
  103. struct snd_ctl_elem_value *ucontrol)
  104. {
  105. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  106. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  107. ucontrol->value.enumerated.item[0] = dspk->mono_to_stereo;
  108. return 0;
  109. }
  110. static int tegra186_dspk_put_mono_to_stereo(struct snd_kcontrol *kcontrol,
  111. struct snd_ctl_elem_value *ucontrol)
  112. {
  113. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  114. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  115. unsigned int value = ucontrol->value.enumerated.item[0];
  116. if (value == dspk->mono_to_stereo)
  117. return 0;
  118. dspk->mono_to_stereo = value;
  119. return 1;
  120. }
  121. static int tegra186_dspk_get_stereo_to_mono(struct snd_kcontrol *kcontrol,
  122. struct snd_ctl_elem_value *ucontrol)
  123. {
  124. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  125. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  126. ucontrol->value.enumerated.item[0] = dspk->stereo_to_mono;
  127. return 0;
  128. }
  129. static int tegra186_dspk_put_stereo_to_mono(struct snd_kcontrol *kcontrol,
  130. struct snd_ctl_elem_value *ucontrol)
  131. {
  132. struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
  133. struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
  134. unsigned int value = ucontrol->value.enumerated.item[0];
  135. if (value == dspk->stereo_to_mono)
  136. return 0;
  137. dspk->stereo_to_mono = value;
  138. return 1;
  139. }
  140. static int __maybe_unused tegra186_dspk_runtime_suspend(struct device *dev)
  141. {
  142. struct tegra186_dspk *dspk = dev_get_drvdata(dev);
  143. regcache_cache_only(dspk->regmap, true);
  144. regcache_mark_dirty(dspk->regmap);
  145. clk_disable_unprepare(dspk->clk_dspk);
  146. return 0;
  147. }
  148. static int __maybe_unused tegra186_dspk_runtime_resume(struct device *dev)
  149. {
  150. struct tegra186_dspk *dspk = dev_get_drvdata(dev);
  151. int err;
  152. err = clk_prepare_enable(dspk->clk_dspk);
  153. if (err) {
  154. dev_err(dev, "failed to enable DSPK clock, err: %d\n", err);
  155. return err;
  156. }
  157. regcache_cache_only(dspk->regmap, false);
  158. regcache_sync(dspk->regmap);
  159. return 0;
  160. }
  161. static int tegra186_dspk_hw_params(struct snd_pcm_substream *substream,
  162. struct snd_pcm_hw_params *params,
  163. struct snd_soc_dai *dai)
  164. {
  165. struct tegra186_dspk *dspk = snd_soc_dai_get_drvdata(dai);
  166. unsigned int channels, srate, dspk_clk;
  167. struct device *dev = dai->dev;
  168. struct tegra_cif_conf cif_conf;
  169. unsigned int max_th;
  170. int err;
  171. memset(&cif_conf, 0, sizeof(struct tegra_cif_conf));
  172. channels = params_channels(params);
  173. cif_conf.audio_ch = channels;
  174. /* Client channel */
  175. switch (dspk->ch_sel) {
  176. case DSPK_CH_SELECT_LEFT:
  177. case DSPK_CH_SELECT_RIGHT:
  178. cif_conf.client_ch = 1;
  179. break;
  180. case DSPK_CH_SELECT_STEREO:
  181. cif_conf.client_ch = 2;
  182. break;
  183. default:
  184. dev_err(dev, "Invalid DSPK client channels\n");
  185. return -EINVAL;
  186. }
  187. cif_conf.client_bits = TEGRA_ACIF_BITS_24;
  188. switch (params_format(params)) {
  189. case SNDRV_PCM_FORMAT_S16_LE:
  190. cif_conf.audio_bits = TEGRA_ACIF_BITS_16;
  191. break;
  192. case SNDRV_PCM_FORMAT_S32_LE:
  193. cif_conf.audio_bits = TEGRA_ACIF_BITS_32;
  194. break;
  195. default:
  196. dev_err(dev, "unsupported format!\n");
  197. return -EOPNOTSUPP;
  198. }
  199. srate = params_rate(params);
  200. /* RX FIFO threshold in terms of frames */
  201. max_th = (TEGRA186_DSPK_RX_FIFO_DEPTH / cif_conf.audio_ch) - 1;
  202. if (dspk->rx_fifo_th > max_th)
  203. dspk->rx_fifo_th = max_th;
  204. cif_conf.threshold = dspk->rx_fifo_th;
  205. cif_conf.mono_conv = dspk->mono_to_stereo;
  206. cif_conf.stereo_conv = dspk->stereo_to_mono;
  207. tegra_set_cif(dspk->regmap, TEGRA186_DSPK_RX_CIF_CTRL,
  208. &cif_conf);
  209. /*
  210. * DSPK clock and PDM codec clock should be synchronous with 4:1 ratio,
  211. * this is because it takes 4 clock cycles to send out one sample to
  212. * codec by sigma delta modulator. Finally the clock rate is a multiple
  213. * of 'Over Sampling Ratio', 'Sample Rate' and 'Interface Clock Ratio'.
  214. */
  215. dspk_clk = (DSPK_OSR_FACTOR << dspk->osr_val) * srate * DSPK_CLK_RATIO;
  216. err = clk_set_rate(dspk->clk_dspk, dspk_clk);
  217. if (err) {
  218. dev_err(dev, "can't set DSPK clock rate %u, err: %d\n",
  219. dspk_clk, err);
  220. return err;
  221. }
  222. regmap_update_bits(dspk->regmap,
  223. /* Reg */
  224. TEGRA186_DSPK_CORE_CTRL,
  225. /* Mask */
  226. TEGRA186_DSPK_OSR_MASK |
  227. TEGRA186_DSPK_CHANNEL_SELECT_MASK |
  228. TEGRA186_DSPK_CTRL_LRSEL_POLARITY_MASK,
  229. /* Value */
  230. (dspk->osr_val << DSPK_OSR_SHIFT) |
  231. ((dspk->ch_sel + 1) << CH_SEL_SHIFT) |
  232. (dspk->lrsel << LRSEL_POL_SHIFT));
  233. return 0;
  234. }
  235. static const struct snd_soc_dai_ops tegra186_dspk_dai_ops = {
  236. .hw_params = tegra186_dspk_hw_params,
  237. };
  238. static struct snd_soc_dai_driver tegra186_dspk_dais[] = {
  239. {
  240. .name = "DSPK-CIF",
  241. .playback = {
  242. .stream_name = "CIF-Playback",
  243. .channels_min = 1,
  244. .channels_max = 2,
  245. .rates = SNDRV_PCM_RATE_8000_48000,
  246. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  247. SNDRV_PCM_FMTBIT_S32_LE,
  248. },
  249. },
  250. {
  251. .name = "DSPK-DAP",
  252. .playback = {
  253. .stream_name = "DAP-Playback",
  254. .channels_min = 1,
  255. .channels_max = 2,
  256. .rates = SNDRV_PCM_RATE_8000_48000,
  257. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  258. SNDRV_PCM_FMTBIT_S32_LE,
  259. },
  260. .ops = &tegra186_dspk_dai_ops,
  261. .symmetric_rate = 1,
  262. },
  263. };
  264. static const struct snd_soc_dapm_widget tegra186_dspk_widgets[] = {
  265. SND_SOC_DAPM_AIF_IN("RX", NULL, 0, TEGRA186_DSPK_ENABLE, 0, 0),
  266. SND_SOC_DAPM_SPK("SPK", NULL),
  267. };
  268. static const struct snd_soc_dapm_route tegra186_dspk_routes[] = {
  269. { "XBAR-Playback", NULL, "XBAR-TX" },
  270. { "CIF-Playback", NULL, "XBAR-Playback" },
  271. { "RX", NULL, "CIF-Playback" },
  272. { "DAP-Playback", NULL, "RX" },
  273. { "SPK", NULL, "DAP-Playback" },
  274. };
  275. static const char * const tegra186_dspk_ch_sel_text[] = {
  276. "Left", "Right", "Stereo",
  277. };
  278. static const struct soc_enum tegra186_dspk_ch_sel_enum =
  279. SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(tegra186_dspk_ch_sel_text),
  280. tegra186_dspk_ch_sel_text);
  281. static const char * const tegra186_dspk_osr_text[] = {
  282. "OSR_32", "OSR_64", "OSR_128", "OSR_256",
  283. };
  284. static const struct soc_enum tegra186_dspk_osr_enum =
  285. SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(tegra186_dspk_osr_text),
  286. tegra186_dspk_osr_text);
  287. static const char * const tegra186_dspk_lrsel_text[] = {
  288. "Left", "Right",
  289. };
  290. static const char * const tegra186_dspk_mono_conv_text[] = {
  291. "Zero", "Copy",
  292. };
  293. static const struct soc_enum tegra186_dspk_mono_conv_enum =
  294. SOC_ENUM_SINGLE(SND_SOC_NOPM, 0,
  295. ARRAY_SIZE(tegra186_dspk_mono_conv_text),
  296. tegra186_dspk_mono_conv_text);
  297. static const char * const tegra186_dspk_stereo_conv_text[] = {
  298. "CH0", "CH1", "AVG",
  299. };
  300. static const struct soc_enum tegra186_dspk_stereo_conv_enum =
  301. SOC_ENUM_SINGLE(SND_SOC_NOPM, 0,
  302. ARRAY_SIZE(tegra186_dspk_stereo_conv_text),
  303. tegra186_dspk_stereo_conv_text);
  304. static const struct soc_enum tegra186_dspk_lrsel_enum =
  305. SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(tegra186_dspk_lrsel_text),
  306. tegra186_dspk_lrsel_text);
  307. static const struct snd_kcontrol_new tegrat186_dspk_controls[] = {
  308. SOC_SINGLE_EXT("FIFO Threshold", SND_SOC_NOPM, 0,
  309. TEGRA186_DSPK_RX_FIFO_DEPTH - 1, 0,
  310. tegra186_dspk_get_fifo_th, tegra186_dspk_put_fifo_th),
  311. SOC_ENUM_EXT("OSR Value", tegra186_dspk_osr_enum,
  312. tegra186_dspk_get_osr_val, tegra186_dspk_put_osr_val),
  313. SOC_ENUM_EXT("LR Polarity Select", tegra186_dspk_lrsel_enum,
  314. tegra186_dspk_get_pol_sel, tegra186_dspk_put_pol_sel),
  315. SOC_ENUM_EXT("Channel Select", tegra186_dspk_ch_sel_enum,
  316. tegra186_dspk_get_ch_sel, tegra186_dspk_put_ch_sel),
  317. SOC_ENUM_EXT("Mono To Stereo", tegra186_dspk_mono_conv_enum,
  318. tegra186_dspk_get_mono_to_stereo,
  319. tegra186_dspk_put_mono_to_stereo),
  320. SOC_ENUM_EXT("Stereo To Mono", tegra186_dspk_stereo_conv_enum,
  321. tegra186_dspk_get_stereo_to_mono,
  322. tegra186_dspk_put_stereo_to_mono),
  323. };
  324. static const struct snd_soc_component_driver tegra186_dspk_cmpnt = {
  325. .dapm_widgets = tegra186_dspk_widgets,
  326. .num_dapm_widgets = ARRAY_SIZE(tegra186_dspk_widgets),
  327. .dapm_routes = tegra186_dspk_routes,
  328. .num_dapm_routes = ARRAY_SIZE(tegra186_dspk_routes),
  329. .controls = tegrat186_dspk_controls,
  330. .num_controls = ARRAY_SIZE(tegrat186_dspk_controls),
  331. };
  332. static bool tegra186_dspk_wr_reg(struct device *dev, unsigned int reg)
  333. {
  334. switch (reg) {
  335. case TEGRA186_DSPK_RX_INT_MASK ... TEGRA186_DSPK_RX_CIF_CTRL:
  336. case TEGRA186_DSPK_ENABLE ... TEGRA186_DSPK_CG:
  337. case TEGRA186_DSPK_CORE_CTRL ... TEGRA186_DSPK_CODEC_CTRL:
  338. return true;
  339. default:
  340. return false;
  341. }
  342. }
  343. static bool tegra186_dspk_rd_reg(struct device *dev, unsigned int reg)
  344. {
  345. if (tegra186_dspk_wr_reg(dev, reg))
  346. return true;
  347. switch (reg) {
  348. case TEGRA186_DSPK_RX_STATUS:
  349. case TEGRA186_DSPK_RX_INT_STATUS:
  350. case TEGRA186_DSPK_STATUS:
  351. case TEGRA186_DSPK_INT_STATUS:
  352. return true;
  353. default:
  354. return false;
  355. }
  356. }
  357. static bool tegra186_dspk_volatile_reg(struct device *dev, unsigned int reg)
  358. {
  359. switch (reg) {
  360. case TEGRA186_DSPK_RX_STATUS:
  361. case TEGRA186_DSPK_RX_INT_STATUS:
  362. case TEGRA186_DSPK_STATUS:
  363. case TEGRA186_DSPK_INT_STATUS:
  364. return true;
  365. default:
  366. return false;
  367. }
  368. }
  369. static const struct regmap_config tegra186_dspk_regmap = {
  370. .reg_bits = 32,
  371. .reg_stride = 4,
  372. .val_bits = 32,
  373. .max_register = TEGRA186_DSPK_CODEC_CTRL,
  374. .writeable_reg = tegra186_dspk_wr_reg,
  375. .readable_reg = tegra186_dspk_rd_reg,
  376. .volatile_reg = tegra186_dspk_volatile_reg,
  377. .reg_defaults = tegra186_dspk_reg_defaults,
  378. .num_reg_defaults = ARRAY_SIZE(tegra186_dspk_reg_defaults),
  379. .cache_type = REGCACHE_FLAT,
  380. };
  381. static const struct of_device_id tegra186_dspk_of_match[] = {
  382. { .compatible = "nvidia,tegra186-dspk" },
  383. {},
  384. };
  385. MODULE_DEVICE_TABLE(of, tegra186_dspk_of_match);
  386. static int tegra186_dspk_platform_probe(struct platform_device *pdev)
  387. {
  388. struct device *dev = &pdev->dev;
  389. struct tegra186_dspk *dspk;
  390. void __iomem *regs;
  391. int err;
  392. dspk = devm_kzalloc(dev, sizeof(*dspk), GFP_KERNEL);
  393. if (!dspk)
  394. return -ENOMEM;
  395. dspk->osr_val = DSPK_OSR_64;
  396. dspk->lrsel = DSPK_LRSEL_LEFT;
  397. dspk->ch_sel = DSPK_CH_SELECT_STEREO;
  398. dspk->mono_to_stereo = 0; /* "Zero" */
  399. dev_set_drvdata(dev, dspk);
  400. dspk->clk_dspk = devm_clk_get(dev, "dspk");
  401. if (IS_ERR(dspk->clk_dspk)) {
  402. dev_err(dev, "can't retrieve DSPK clock\n");
  403. return PTR_ERR(dspk->clk_dspk);
  404. }
  405. regs = devm_platform_ioremap_resource(pdev, 0);
  406. if (IS_ERR(regs))
  407. return PTR_ERR(regs);
  408. dspk->regmap = devm_regmap_init_mmio(dev, regs, &tegra186_dspk_regmap);
  409. if (IS_ERR(dspk->regmap)) {
  410. dev_err(dev, "regmap init failed\n");
  411. return PTR_ERR(dspk->regmap);
  412. }
  413. regcache_cache_only(dspk->regmap, true);
  414. err = devm_snd_soc_register_component(dev, &tegra186_dspk_cmpnt,
  415. tegra186_dspk_dais,
  416. ARRAY_SIZE(tegra186_dspk_dais));
  417. if (err) {
  418. dev_err(dev, "can't register DSPK component, err: %d\n",
  419. err);
  420. return err;
  421. }
  422. pm_runtime_enable(dev);
  423. return 0;
  424. }
  425. static int tegra186_dspk_platform_remove(struct platform_device *pdev)
  426. {
  427. pm_runtime_disable(&pdev->dev);
  428. return 0;
  429. }
  430. static const struct dev_pm_ops tegra186_dspk_pm_ops = {
  431. SET_RUNTIME_PM_OPS(tegra186_dspk_runtime_suspend,
  432. tegra186_dspk_runtime_resume, NULL)
  433. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  434. pm_runtime_force_resume)
  435. };
  436. static struct platform_driver tegra186_dspk_driver = {
  437. .driver = {
  438. .name = "tegra186-dspk",
  439. .of_match_table = tegra186_dspk_of_match,
  440. .pm = &tegra186_dspk_pm_ops,
  441. },
  442. .probe = tegra186_dspk_platform_probe,
  443. .remove = tegra186_dspk_platform_remove,
  444. };
  445. module_platform_driver(tegra186_dspk_driver);
  446. MODULE_AUTHOR("Mohan Kumar <[email protected]>");
  447. MODULE_AUTHOR("Sameer Pujar <[email protected]>");
  448. MODULE_DESCRIPTION("Tegra186 ASoC DSPK driver");
  449. MODULE_LICENSE("GPL v2");