tegra210_i2s.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // tegra210_i2s.c - Tegra210 I2S 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_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/regmap.h>
  13. #include <sound/core.h>
  14. #include <sound/pcm_params.h>
  15. #include <sound/soc.h>
  16. #include "tegra210_i2s.h"
  17. #include "tegra_cif.h"
  18. static const struct reg_default tegra210_i2s_reg_defaults[] = {
  19. { TEGRA210_I2S_RX_INT_MASK, 0x00000003 },
  20. { TEGRA210_I2S_RX_CIF_CTRL, 0x00007700 },
  21. { TEGRA210_I2S_TX_INT_MASK, 0x00000003 },
  22. { TEGRA210_I2S_TX_CIF_CTRL, 0x00007700 },
  23. { TEGRA210_I2S_CG, 0x1 },
  24. { TEGRA210_I2S_TIMING, 0x0000001f },
  25. { TEGRA210_I2S_ENABLE, 0x1 },
  26. /*
  27. * Below update does not have any effect on Tegra186 and Tegra194.
  28. * On Tegra210, I2S4 has "i2s4a" and "i2s4b" pins and below update
  29. * is required to select i2s4b for it to be functional for I2S
  30. * operation.
  31. */
  32. { TEGRA210_I2S_CYA, 0x1 },
  33. };
  34. static void tegra210_i2s_set_slot_ctrl(struct regmap *regmap,
  35. unsigned int total_slots,
  36. unsigned int tx_slot_mask,
  37. unsigned int rx_slot_mask)
  38. {
  39. regmap_write(regmap, TEGRA210_I2S_SLOT_CTRL, total_slots - 1);
  40. regmap_write(regmap, TEGRA210_I2S_TX_SLOT_CTRL, tx_slot_mask);
  41. regmap_write(regmap, TEGRA210_I2S_RX_SLOT_CTRL, rx_slot_mask);
  42. }
  43. static int tegra210_i2s_set_clock_rate(struct device *dev,
  44. unsigned int clock_rate)
  45. {
  46. struct tegra210_i2s *i2s = dev_get_drvdata(dev);
  47. unsigned int val;
  48. int err;
  49. regmap_read(i2s->regmap, TEGRA210_I2S_CTRL, &val);
  50. /* No need to set rates if I2S is being operated in slave */
  51. if (!(val & I2S_CTRL_MASTER_EN))
  52. return 0;
  53. err = clk_set_rate(i2s->clk_i2s, clock_rate);
  54. if (err) {
  55. dev_err(dev, "can't set I2S bit clock rate %u, err: %d\n",
  56. clock_rate, err);
  57. return err;
  58. }
  59. if (!IS_ERR(i2s->clk_sync_input)) {
  60. /*
  61. * Other I/O modules in AHUB can use i2s bclk as reference
  62. * clock. Below sets sync input clock rate as per bclk,
  63. * which can be used as input to other I/O modules.
  64. */
  65. err = clk_set_rate(i2s->clk_sync_input, clock_rate);
  66. if (err) {
  67. dev_err(dev,
  68. "can't set I2S sync input rate %u, err = %d\n",
  69. clock_rate, err);
  70. return err;
  71. }
  72. }
  73. return 0;
  74. }
  75. static int tegra210_i2s_sw_reset(struct snd_soc_component *compnt,
  76. bool is_playback)
  77. {
  78. struct device *dev = compnt->dev;
  79. struct tegra210_i2s *i2s = dev_get_drvdata(dev);
  80. unsigned int reset_mask = I2S_SOFT_RESET_MASK;
  81. unsigned int reset_en = I2S_SOFT_RESET_EN;
  82. unsigned int reset_reg, cif_reg, stream_reg;
  83. unsigned int cif_ctrl, stream_ctrl, i2s_ctrl, val;
  84. int err;
  85. if (is_playback) {
  86. reset_reg = TEGRA210_I2S_RX_SOFT_RESET;
  87. cif_reg = TEGRA210_I2S_RX_CIF_CTRL;
  88. stream_reg = TEGRA210_I2S_RX_CTRL;
  89. } else {
  90. reset_reg = TEGRA210_I2S_TX_SOFT_RESET;
  91. cif_reg = TEGRA210_I2S_TX_CIF_CTRL;
  92. stream_reg = TEGRA210_I2S_TX_CTRL;
  93. }
  94. /* Store CIF and I2S control values */
  95. regmap_read(i2s->regmap, cif_reg, &cif_ctrl);
  96. regmap_read(i2s->regmap, stream_reg, &stream_ctrl);
  97. regmap_read(i2s->regmap, TEGRA210_I2S_CTRL, &i2s_ctrl);
  98. /* Reset to make sure the previous transactions are clean */
  99. regmap_update_bits(i2s->regmap, reset_reg, reset_mask, reset_en);
  100. err = regmap_read_poll_timeout(i2s->regmap, reset_reg, val,
  101. !(val & reset_mask & reset_en),
  102. 10, 10000);
  103. if (err) {
  104. dev_err(dev, "timeout: failed to reset I2S for %s\n",
  105. is_playback ? "playback" : "capture");
  106. return err;
  107. }
  108. /* Restore CIF and I2S control values */
  109. regmap_write(i2s->regmap, cif_reg, cif_ctrl);
  110. regmap_write(i2s->regmap, stream_reg, stream_ctrl);
  111. regmap_write(i2s->regmap, TEGRA210_I2S_CTRL, i2s_ctrl);
  112. return 0;
  113. }
  114. static int tegra210_i2s_init(struct snd_soc_dapm_widget *w,
  115. struct snd_kcontrol *kcontrol, int event)
  116. {
  117. struct snd_soc_component *compnt = snd_soc_dapm_to_component(w->dapm);
  118. struct device *dev = compnt->dev;
  119. struct tegra210_i2s *i2s = dev_get_drvdata(dev);
  120. unsigned int val, status_reg;
  121. bool is_playback;
  122. int err;
  123. switch (w->reg) {
  124. case TEGRA210_I2S_RX_ENABLE:
  125. is_playback = true;
  126. status_reg = TEGRA210_I2S_RX_STATUS;
  127. break;
  128. case TEGRA210_I2S_TX_ENABLE:
  129. is_playback = false;
  130. status_reg = TEGRA210_I2S_TX_STATUS;
  131. break;
  132. default:
  133. return -EINVAL;
  134. }
  135. /* Ensure I2S is in disabled state before new session */
  136. err = regmap_read_poll_timeout(i2s->regmap, status_reg, val,
  137. !(val & I2S_EN_MASK & I2S_EN),
  138. 10, 10000);
  139. if (err) {
  140. dev_err(dev, "timeout: previous I2S %s is still active\n",
  141. is_playback ? "playback" : "capture");
  142. return err;
  143. }
  144. return tegra210_i2s_sw_reset(compnt, is_playback);
  145. }
  146. static int __maybe_unused tegra210_i2s_runtime_suspend(struct device *dev)
  147. {
  148. struct tegra210_i2s *i2s = dev_get_drvdata(dev);
  149. regcache_cache_only(i2s->regmap, true);
  150. regcache_mark_dirty(i2s->regmap);
  151. clk_disable_unprepare(i2s->clk_i2s);
  152. return 0;
  153. }
  154. static int __maybe_unused tegra210_i2s_runtime_resume(struct device *dev)
  155. {
  156. struct tegra210_i2s *i2s = dev_get_drvdata(dev);
  157. int err;
  158. err = clk_prepare_enable(i2s->clk_i2s);
  159. if (err) {
  160. dev_err(dev, "failed to enable I2S bit clock, err: %d\n", err);
  161. return err;
  162. }
  163. regcache_cache_only(i2s->regmap, false);
  164. regcache_sync(i2s->regmap);
  165. return 0;
  166. }
  167. static void tegra210_i2s_set_data_offset(struct tegra210_i2s *i2s,
  168. unsigned int data_offset)
  169. {
  170. /* Capture path */
  171. regmap_update_bits(i2s->regmap, TEGRA210_I2S_TX_CTRL,
  172. I2S_CTRL_DATA_OFFSET_MASK,
  173. data_offset << I2S_DATA_SHIFT);
  174. /* Playback path */
  175. regmap_update_bits(i2s->regmap, TEGRA210_I2S_RX_CTRL,
  176. I2S_CTRL_DATA_OFFSET_MASK,
  177. data_offset << I2S_DATA_SHIFT);
  178. }
  179. static int tegra210_i2s_set_fmt(struct snd_soc_dai *dai,
  180. unsigned int fmt)
  181. {
  182. struct tegra210_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  183. unsigned int mask, val;
  184. mask = I2S_CTRL_MASTER_EN_MASK;
  185. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  186. case SND_SOC_DAIFMT_BC_FC:
  187. val = 0;
  188. break;
  189. case SND_SOC_DAIFMT_BP_FP:
  190. val = I2S_CTRL_MASTER_EN;
  191. break;
  192. default:
  193. return -EINVAL;
  194. }
  195. mask |= I2S_CTRL_FRAME_FMT_MASK | I2S_CTRL_LRCK_POL_MASK;
  196. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  197. case SND_SOC_DAIFMT_DSP_A:
  198. val |= I2S_CTRL_FRAME_FMT_FSYNC_MODE;
  199. val |= I2S_CTRL_LRCK_POL_HIGH;
  200. tegra210_i2s_set_data_offset(i2s, 1);
  201. break;
  202. case SND_SOC_DAIFMT_DSP_B:
  203. val |= I2S_CTRL_FRAME_FMT_FSYNC_MODE;
  204. val |= I2S_CTRL_LRCK_POL_HIGH;
  205. tegra210_i2s_set_data_offset(i2s, 0);
  206. break;
  207. /* I2S mode has data offset of 1 */
  208. case SND_SOC_DAIFMT_I2S:
  209. val |= I2S_CTRL_FRAME_FMT_LRCK_MODE;
  210. val |= I2S_CTRL_LRCK_POL_LOW;
  211. tegra210_i2s_set_data_offset(i2s, 1);
  212. break;
  213. /*
  214. * For RJ mode data offset is dependent on the sample size
  215. * and the bclk ratio, and so is set when hw_params is called.
  216. */
  217. case SND_SOC_DAIFMT_RIGHT_J:
  218. val |= I2S_CTRL_FRAME_FMT_LRCK_MODE;
  219. val |= I2S_CTRL_LRCK_POL_HIGH;
  220. break;
  221. case SND_SOC_DAIFMT_LEFT_J:
  222. val |= I2S_CTRL_FRAME_FMT_LRCK_MODE;
  223. val |= I2S_CTRL_LRCK_POL_HIGH;
  224. tegra210_i2s_set_data_offset(i2s, 0);
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. mask |= I2S_CTRL_EDGE_CTRL_MASK;
  230. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  231. case SND_SOC_DAIFMT_NB_NF:
  232. val |= I2S_CTRL_EDGE_CTRL_POS_EDGE;
  233. break;
  234. case SND_SOC_DAIFMT_NB_IF:
  235. val |= I2S_CTRL_EDGE_CTRL_POS_EDGE;
  236. val ^= I2S_CTRL_LRCK_POL_MASK;
  237. break;
  238. case SND_SOC_DAIFMT_IB_NF:
  239. val |= I2S_CTRL_EDGE_CTRL_NEG_EDGE;
  240. break;
  241. case SND_SOC_DAIFMT_IB_IF:
  242. val |= I2S_CTRL_EDGE_CTRL_NEG_EDGE;
  243. val ^= I2S_CTRL_LRCK_POL_MASK;
  244. break;
  245. default:
  246. return -EINVAL;
  247. }
  248. regmap_update_bits(i2s->regmap, TEGRA210_I2S_CTRL, mask, val);
  249. i2s->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  250. return 0;
  251. }
  252. static int tegra210_i2s_set_tdm_slot(struct snd_soc_dai *dai,
  253. unsigned int tx_mask, unsigned int rx_mask,
  254. int slots, int slot_width)
  255. {
  256. struct tegra210_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  257. /* Copy the required tx and rx mask */
  258. i2s->tx_mask = (tx_mask > DEFAULT_I2S_SLOT_MASK) ?
  259. DEFAULT_I2S_SLOT_MASK : tx_mask;
  260. i2s->rx_mask = (rx_mask > DEFAULT_I2S_SLOT_MASK) ?
  261. DEFAULT_I2S_SLOT_MASK : rx_mask;
  262. return 0;
  263. }
  264. static int tegra210_i2s_get_loopback(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_value *ucontrol)
  266. {
  267. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  268. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  269. ucontrol->value.integer.value[0] = i2s->loopback;
  270. return 0;
  271. }
  272. static int tegra210_i2s_put_loopback(struct snd_kcontrol *kcontrol,
  273. struct snd_ctl_elem_value *ucontrol)
  274. {
  275. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  276. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  277. int value = ucontrol->value.integer.value[0];
  278. if (value == i2s->loopback)
  279. return 0;
  280. i2s->loopback = value;
  281. regmap_update_bits(i2s->regmap, TEGRA210_I2S_CTRL, I2S_CTRL_LPBK_MASK,
  282. i2s->loopback << I2S_CTRL_LPBK_SHIFT);
  283. return 1;
  284. }
  285. static int tegra210_i2s_get_fsync_width(struct snd_kcontrol *kcontrol,
  286. struct snd_ctl_elem_value *ucontrol)
  287. {
  288. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  289. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  290. ucontrol->value.integer.value[0] = i2s->fsync_width;
  291. return 0;
  292. }
  293. static int tegra210_i2s_put_fsync_width(struct snd_kcontrol *kcontrol,
  294. struct snd_ctl_elem_value *ucontrol)
  295. {
  296. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  297. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  298. int value = ucontrol->value.integer.value[0];
  299. if (value == i2s->fsync_width)
  300. return 0;
  301. i2s->fsync_width = value;
  302. /*
  303. * Frame sync width is used only for FSYNC modes and not
  304. * applicable for LRCK modes. Reset value for this field is "0",
  305. * which means the width is one bit clock wide.
  306. * The width requirement may depend on the codec and in such
  307. * cases mixer control is used to update custom values. A value
  308. * of "N" here means, width is "N + 1" bit clock wide.
  309. */
  310. regmap_update_bits(i2s->regmap, TEGRA210_I2S_CTRL,
  311. I2S_CTRL_FSYNC_WIDTH_MASK,
  312. i2s->fsync_width << I2S_FSYNC_WIDTH_SHIFT);
  313. return 1;
  314. }
  315. static int tegra210_i2s_cget_stereo_to_mono(struct snd_kcontrol *kcontrol,
  316. struct snd_ctl_elem_value *ucontrol)
  317. {
  318. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  319. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  320. ucontrol->value.enumerated.item[0] = i2s->stereo_to_mono[I2S_TX_PATH];
  321. return 0;
  322. }
  323. static int tegra210_i2s_cput_stereo_to_mono(struct snd_kcontrol *kcontrol,
  324. struct snd_ctl_elem_value *ucontrol)
  325. {
  326. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  327. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  328. unsigned int value = ucontrol->value.enumerated.item[0];
  329. if (value == i2s->stereo_to_mono[I2S_TX_PATH])
  330. return 0;
  331. i2s->stereo_to_mono[I2S_TX_PATH] = value;
  332. return 1;
  333. }
  334. static int tegra210_i2s_cget_mono_to_stereo(struct snd_kcontrol *kcontrol,
  335. struct snd_ctl_elem_value *ucontrol)
  336. {
  337. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  338. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  339. ucontrol->value.enumerated.item[0] = i2s->mono_to_stereo[I2S_TX_PATH];
  340. return 0;
  341. }
  342. static int tegra210_i2s_cput_mono_to_stereo(struct snd_kcontrol *kcontrol,
  343. struct snd_ctl_elem_value *ucontrol)
  344. {
  345. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  346. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  347. unsigned int value = ucontrol->value.enumerated.item[0];
  348. if (value == i2s->mono_to_stereo[I2S_TX_PATH])
  349. return 0;
  350. i2s->mono_to_stereo[I2S_TX_PATH] = value;
  351. return 1;
  352. }
  353. static int tegra210_i2s_pget_stereo_to_mono(struct snd_kcontrol *kcontrol,
  354. struct snd_ctl_elem_value *ucontrol)
  355. {
  356. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  357. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  358. ucontrol->value.enumerated.item[0] = i2s->stereo_to_mono[I2S_RX_PATH];
  359. return 0;
  360. }
  361. static int tegra210_i2s_pput_stereo_to_mono(struct snd_kcontrol *kcontrol,
  362. struct snd_ctl_elem_value *ucontrol)
  363. {
  364. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  365. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  366. unsigned int value = ucontrol->value.enumerated.item[0];
  367. if (value == i2s->stereo_to_mono[I2S_RX_PATH])
  368. return 0;
  369. i2s->stereo_to_mono[I2S_RX_PATH] = value;
  370. return 1;
  371. }
  372. static int tegra210_i2s_pget_mono_to_stereo(struct snd_kcontrol *kcontrol,
  373. struct snd_ctl_elem_value *ucontrol)
  374. {
  375. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  376. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  377. ucontrol->value.enumerated.item[0] = i2s->mono_to_stereo[I2S_RX_PATH];
  378. return 0;
  379. }
  380. static int tegra210_i2s_pput_mono_to_stereo(struct snd_kcontrol *kcontrol,
  381. struct snd_ctl_elem_value *ucontrol)
  382. {
  383. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  384. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  385. unsigned int value = ucontrol->value.enumerated.item[0];
  386. if (value == i2s->mono_to_stereo[I2S_RX_PATH])
  387. return 0;
  388. i2s->mono_to_stereo[I2S_RX_PATH] = value;
  389. return 1;
  390. }
  391. static int tegra210_i2s_pget_fifo_th(struct snd_kcontrol *kcontrol,
  392. struct snd_ctl_elem_value *ucontrol)
  393. {
  394. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  395. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  396. ucontrol->value.integer.value[0] = i2s->rx_fifo_th;
  397. return 0;
  398. }
  399. static int tegra210_i2s_pput_fifo_th(struct snd_kcontrol *kcontrol,
  400. struct snd_ctl_elem_value *ucontrol)
  401. {
  402. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  403. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  404. int value = ucontrol->value.integer.value[0];
  405. if (value == i2s->rx_fifo_th)
  406. return 0;
  407. i2s->rx_fifo_th = value;
  408. return 1;
  409. }
  410. static int tegra210_i2s_get_bclk_ratio(struct snd_kcontrol *kcontrol,
  411. struct snd_ctl_elem_value *ucontrol)
  412. {
  413. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  414. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  415. ucontrol->value.integer.value[0] = i2s->bclk_ratio;
  416. return 0;
  417. }
  418. static int tegra210_i2s_put_bclk_ratio(struct snd_kcontrol *kcontrol,
  419. struct snd_ctl_elem_value *ucontrol)
  420. {
  421. struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
  422. struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
  423. int value = ucontrol->value.integer.value[0];
  424. if (value == i2s->bclk_ratio)
  425. return 0;
  426. i2s->bclk_ratio = value;
  427. return 1;
  428. }
  429. static int tegra210_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  430. unsigned int ratio)
  431. {
  432. struct tegra210_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  433. i2s->bclk_ratio = ratio;
  434. return 0;
  435. }
  436. static int tegra210_i2s_set_timing_params(struct device *dev,
  437. unsigned int sample_size,
  438. unsigned int srate,
  439. unsigned int channels)
  440. {
  441. struct tegra210_i2s *i2s = dev_get_drvdata(dev);
  442. unsigned int val, bit_count, bclk_rate, num_bclk = sample_size;
  443. int err;
  444. if (i2s->bclk_ratio)
  445. num_bclk *= i2s->bclk_ratio;
  446. if (i2s->dai_fmt == SND_SOC_DAIFMT_RIGHT_J)
  447. tegra210_i2s_set_data_offset(i2s, num_bclk - sample_size);
  448. /* I2S bit clock rate */
  449. bclk_rate = srate * channels * num_bclk;
  450. err = tegra210_i2s_set_clock_rate(dev, bclk_rate);
  451. if (err) {
  452. dev_err(dev, "can't set I2S bit clock rate %u, err: %d\n",
  453. bclk_rate, err);
  454. return err;
  455. }
  456. regmap_read(i2s->regmap, TEGRA210_I2S_CTRL, &val);
  457. /*
  458. * For LRCK mode, channel bit count depends on number of bit clocks
  459. * on the left channel, where as for FSYNC mode bit count depends on
  460. * the number of bit clocks in both left and right channels for DSP
  461. * mode or the number of bit clocks in one TDM frame.
  462. *
  463. */
  464. switch (val & I2S_CTRL_FRAME_FMT_MASK) {
  465. case I2S_CTRL_FRAME_FMT_LRCK_MODE:
  466. bit_count = (bclk_rate / (srate * 2)) - 1;
  467. break;
  468. case I2S_CTRL_FRAME_FMT_FSYNC_MODE:
  469. bit_count = (bclk_rate / srate) - 1;
  470. tegra210_i2s_set_slot_ctrl(i2s->regmap, channels,
  471. i2s->tx_mask, i2s->rx_mask);
  472. break;
  473. default:
  474. dev_err(dev, "invalid I2S frame format\n");
  475. return -EINVAL;
  476. }
  477. if (bit_count > I2S_TIMING_CH_BIT_CNT_MASK) {
  478. dev_err(dev, "invalid I2S channel bit count %u\n", bit_count);
  479. return -EINVAL;
  480. }
  481. regmap_write(i2s->regmap, TEGRA210_I2S_TIMING,
  482. bit_count << I2S_TIMING_CH_BIT_CNT_SHIFT);
  483. return 0;
  484. }
  485. static int tegra210_i2s_hw_params(struct snd_pcm_substream *substream,
  486. struct snd_pcm_hw_params *params,
  487. struct snd_soc_dai *dai)
  488. {
  489. struct device *dev = dai->dev;
  490. struct tegra210_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  491. unsigned int sample_size, channels, srate, val, reg, path;
  492. struct tegra_cif_conf cif_conf;
  493. memset(&cif_conf, 0, sizeof(struct tegra_cif_conf));
  494. channels = params_channels(params);
  495. if (channels < 1) {
  496. dev_err(dev, "invalid I2S %d channel configuration\n",
  497. channels);
  498. return -EINVAL;
  499. }
  500. cif_conf.audio_ch = channels;
  501. cif_conf.client_ch = channels;
  502. switch (params_format(params)) {
  503. case SNDRV_PCM_FORMAT_S8:
  504. val = I2S_BITS_8;
  505. sample_size = 8;
  506. cif_conf.audio_bits = TEGRA_ACIF_BITS_8;
  507. cif_conf.client_bits = TEGRA_ACIF_BITS_8;
  508. break;
  509. case SNDRV_PCM_FORMAT_S16_LE:
  510. val = I2S_BITS_16;
  511. sample_size = 16;
  512. cif_conf.audio_bits = TEGRA_ACIF_BITS_16;
  513. cif_conf.client_bits = TEGRA_ACIF_BITS_16;
  514. break;
  515. case SNDRV_PCM_FORMAT_S32_LE:
  516. val = I2S_BITS_32;
  517. sample_size = 32;
  518. cif_conf.audio_bits = TEGRA_ACIF_BITS_32;
  519. cif_conf.client_bits = TEGRA_ACIF_BITS_32;
  520. break;
  521. default:
  522. dev_err(dev, "unsupported format!\n");
  523. return -EOPNOTSUPP;
  524. }
  525. /* Program sample size */
  526. regmap_update_bits(i2s->regmap, TEGRA210_I2S_CTRL,
  527. I2S_CTRL_BIT_SIZE_MASK, val);
  528. srate = params_rate(params);
  529. /* For playback I2S RX-CIF and for capture TX-CIF is used */
  530. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  531. path = I2S_RX_PATH;
  532. else
  533. path = I2S_TX_PATH;
  534. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  535. unsigned int max_th;
  536. /* FIFO threshold in terms of frames */
  537. max_th = (I2S_RX_FIFO_DEPTH / cif_conf.audio_ch) - 1;
  538. if (i2s->rx_fifo_th > max_th)
  539. i2s->rx_fifo_th = max_th;
  540. cif_conf.threshold = i2s->rx_fifo_th;
  541. reg = TEGRA210_I2S_RX_CIF_CTRL;
  542. } else {
  543. reg = TEGRA210_I2S_TX_CIF_CTRL;
  544. }
  545. cif_conf.mono_conv = i2s->mono_to_stereo[path];
  546. cif_conf.stereo_conv = i2s->stereo_to_mono[path];
  547. tegra_set_cif(i2s->regmap, reg, &cif_conf);
  548. return tegra210_i2s_set_timing_params(dev, sample_size, srate,
  549. cif_conf.client_ch);
  550. }
  551. static const struct snd_soc_dai_ops tegra210_i2s_dai_ops = {
  552. .set_fmt = tegra210_i2s_set_fmt,
  553. .hw_params = tegra210_i2s_hw_params,
  554. .set_bclk_ratio = tegra210_i2s_set_dai_bclk_ratio,
  555. .set_tdm_slot = tegra210_i2s_set_tdm_slot,
  556. };
  557. static struct snd_soc_dai_driver tegra210_i2s_dais[] = {
  558. {
  559. .name = "I2S-CIF",
  560. .playback = {
  561. .stream_name = "CIF-Playback",
  562. .channels_min = 1,
  563. .channels_max = 16,
  564. .rates = SNDRV_PCM_RATE_8000_192000,
  565. .formats = SNDRV_PCM_FMTBIT_S8 |
  566. SNDRV_PCM_FMTBIT_S16_LE |
  567. SNDRV_PCM_FMTBIT_S32_LE,
  568. },
  569. .capture = {
  570. .stream_name = "CIF-Capture",
  571. .channels_min = 1,
  572. .channels_max = 16,
  573. .rates = SNDRV_PCM_RATE_8000_192000,
  574. .formats = SNDRV_PCM_FMTBIT_S8 |
  575. SNDRV_PCM_FMTBIT_S16_LE |
  576. SNDRV_PCM_FMTBIT_S32_LE,
  577. },
  578. },
  579. {
  580. .name = "I2S-DAP",
  581. .playback = {
  582. .stream_name = "DAP-Playback",
  583. .channels_min = 1,
  584. .channels_max = 16,
  585. .rates = SNDRV_PCM_RATE_8000_192000,
  586. .formats = SNDRV_PCM_FMTBIT_S8 |
  587. SNDRV_PCM_FMTBIT_S16_LE |
  588. SNDRV_PCM_FMTBIT_S32_LE,
  589. },
  590. .capture = {
  591. .stream_name = "DAP-Capture",
  592. .channels_min = 1,
  593. .channels_max = 16,
  594. .rates = SNDRV_PCM_RATE_8000_192000,
  595. .formats = SNDRV_PCM_FMTBIT_S8 |
  596. SNDRV_PCM_FMTBIT_S16_LE |
  597. SNDRV_PCM_FMTBIT_S32_LE,
  598. },
  599. .ops = &tegra210_i2s_dai_ops,
  600. .symmetric_rate = 1,
  601. },
  602. };
  603. static const char * const tegra210_i2s_stereo_conv_text[] = {
  604. "CH0", "CH1", "AVG",
  605. };
  606. static const char * const tegra210_i2s_mono_conv_text[] = {
  607. "Zero", "Copy",
  608. };
  609. static const struct soc_enum tegra210_i2s_mono_conv_enum =
  610. SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_i2s_mono_conv_text),
  611. tegra210_i2s_mono_conv_text);
  612. static const struct soc_enum tegra210_i2s_stereo_conv_enum =
  613. SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_i2s_stereo_conv_text),
  614. tegra210_i2s_stereo_conv_text);
  615. static const struct snd_kcontrol_new tegra210_i2s_controls[] = {
  616. SOC_SINGLE_EXT("Loopback", 0, 0, 1, 0, tegra210_i2s_get_loopback,
  617. tegra210_i2s_put_loopback),
  618. SOC_SINGLE_EXT("FSYNC Width", 0, 0, 255, 0,
  619. tegra210_i2s_get_fsync_width,
  620. tegra210_i2s_put_fsync_width),
  621. SOC_ENUM_EXT("Capture Stereo To Mono", tegra210_i2s_stereo_conv_enum,
  622. tegra210_i2s_cget_stereo_to_mono,
  623. tegra210_i2s_cput_stereo_to_mono),
  624. SOC_ENUM_EXT("Capture Mono To Stereo", tegra210_i2s_mono_conv_enum,
  625. tegra210_i2s_cget_mono_to_stereo,
  626. tegra210_i2s_cput_mono_to_stereo),
  627. SOC_ENUM_EXT("Playback Stereo To Mono", tegra210_i2s_stereo_conv_enum,
  628. tegra210_i2s_pget_mono_to_stereo,
  629. tegra210_i2s_pput_mono_to_stereo),
  630. SOC_ENUM_EXT("Playback Mono To Stereo", tegra210_i2s_mono_conv_enum,
  631. tegra210_i2s_pget_stereo_to_mono,
  632. tegra210_i2s_pput_stereo_to_mono),
  633. SOC_SINGLE_EXT("Playback FIFO Threshold", 0, 0, I2S_RX_FIFO_DEPTH - 1,
  634. 0, tegra210_i2s_pget_fifo_th, tegra210_i2s_pput_fifo_th),
  635. SOC_SINGLE_EXT("BCLK Ratio", 0, 0, INT_MAX, 0,
  636. tegra210_i2s_get_bclk_ratio,
  637. tegra210_i2s_put_bclk_ratio),
  638. };
  639. static const struct snd_soc_dapm_widget tegra210_i2s_widgets[] = {
  640. SND_SOC_DAPM_AIF_IN_E("RX", NULL, 0, TEGRA210_I2S_RX_ENABLE,
  641. 0, 0, tegra210_i2s_init, SND_SOC_DAPM_PRE_PMU),
  642. SND_SOC_DAPM_AIF_OUT_E("TX", NULL, 0, TEGRA210_I2S_TX_ENABLE,
  643. 0, 0, tegra210_i2s_init, SND_SOC_DAPM_PRE_PMU),
  644. SND_SOC_DAPM_MIC("MIC", NULL),
  645. SND_SOC_DAPM_SPK("SPK", NULL),
  646. };
  647. static const struct snd_soc_dapm_route tegra210_i2s_routes[] = {
  648. /* Playback route from XBAR */
  649. { "XBAR-Playback", NULL, "XBAR-TX" },
  650. { "CIF-Playback", NULL, "XBAR-Playback" },
  651. { "RX", NULL, "CIF-Playback" },
  652. { "DAP-Playback", NULL, "RX" },
  653. { "SPK", NULL, "DAP-Playback" },
  654. /* Capture route to XBAR */
  655. { "XBAR-RX", NULL, "XBAR-Capture" },
  656. { "XBAR-Capture", NULL, "CIF-Capture" },
  657. { "CIF-Capture", NULL, "TX" },
  658. { "TX", NULL, "DAP-Capture" },
  659. { "DAP-Capture", NULL, "MIC" },
  660. };
  661. static const struct snd_soc_component_driver tegra210_i2s_cmpnt = {
  662. .dapm_widgets = tegra210_i2s_widgets,
  663. .num_dapm_widgets = ARRAY_SIZE(tegra210_i2s_widgets),
  664. .dapm_routes = tegra210_i2s_routes,
  665. .num_dapm_routes = ARRAY_SIZE(tegra210_i2s_routes),
  666. .controls = tegra210_i2s_controls,
  667. .num_controls = ARRAY_SIZE(tegra210_i2s_controls),
  668. };
  669. static bool tegra210_i2s_wr_reg(struct device *dev, unsigned int reg)
  670. {
  671. switch (reg) {
  672. case TEGRA210_I2S_RX_ENABLE ... TEGRA210_I2S_RX_SOFT_RESET:
  673. case TEGRA210_I2S_RX_INT_MASK ... TEGRA210_I2S_RX_CLK_TRIM:
  674. case TEGRA210_I2S_TX_ENABLE ... TEGRA210_I2S_TX_SOFT_RESET:
  675. case TEGRA210_I2S_TX_INT_MASK ... TEGRA210_I2S_TX_CLK_TRIM:
  676. case TEGRA210_I2S_ENABLE ... TEGRA210_I2S_CG:
  677. case TEGRA210_I2S_CTRL ... TEGRA210_I2S_CYA:
  678. return true;
  679. default:
  680. return false;
  681. }
  682. }
  683. static bool tegra210_i2s_rd_reg(struct device *dev, unsigned int reg)
  684. {
  685. if (tegra210_i2s_wr_reg(dev, reg))
  686. return true;
  687. switch (reg) {
  688. case TEGRA210_I2S_RX_STATUS:
  689. case TEGRA210_I2S_RX_INT_STATUS:
  690. case TEGRA210_I2S_RX_CIF_FIFO_STATUS:
  691. case TEGRA210_I2S_TX_STATUS:
  692. case TEGRA210_I2S_TX_INT_STATUS:
  693. case TEGRA210_I2S_TX_CIF_FIFO_STATUS:
  694. case TEGRA210_I2S_STATUS:
  695. case TEGRA210_I2S_INT_STATUS:
  696. return true;
  697. default:
  698. return false;
  699. }
  700. }
  701. static bool tegra210_i2s_volatile_reg(struct device *dev, unsigned int reg)
  702. {
  703. switch (reg) {
  704. case TEGRA210_I2S_RX_STATUS:
  705. case TEGRA210_I2S_RX_INT_STATUS:
  706. case TEGRA210_I2S_RX_CIF_FIFO_STATUS:
  707. case TEGRA210_I2S_TX_STATUS:
  708. case TEGRA210_I2S_TX_INT_STATUS:
  709. case TEGRA210_I2S_TX_CIF_FIFO_STATUS:
  710. case TEGRA210_I2S_STATUS:
  711. case TEGRA210_I2S_INT_STATUS:
  712. case TEGRA210_I2S_RX_SOFT_RESET:
  713. case TEGRA210_I2S_TX_SOFT_RESET:
  714. return true;
  715. default:
  716. return false;
  717. }
  718. }
  719. static const struct regmap_config tegra210_i2s_regmap_config = {
  720. .reg_bits = 32,
  721. .reg_stride = 4,
  722. .val_bits = 32,
  723. .max_register = TEGRA210_I2S_CYA,
  724. .writeable_reg = tegra210_i2s_wr_reg,
  725. .readable_reg = tegra210_i2s_rd_reg,
  726. .volatile_reg = tegra210_i2s_volatile_reg,
  727. .reg_defaults = tegra210_i2s_reg_defaults,
  728. .num_reg_defaults = ARRAY_SIZE(tegra210_i2s_reg_defaults),
  729. .cache_type = REGCACHE_FLAT,
  730. };
  731. static int tegra210_i2s_probe(struct platform_device *pdev)
  732. {
  733. struct device *dev = &pdev->dev;
  734. struct tegra210_i2s *i2s;
  735. void __iomem *regs;
  736. int err;
  737. i2s = devm_kzalloc(dev, sizeof(*i2s), GFP_KERNEL);
  738. if (!i2s)
  739. return -ENOMEM;
  740. i2s->rx_fifo_th = DEFAULT_I2S_RX_FIFO_THRESHOLD;
  741. i2s->tx_mask = DEFAULT_I2S_SLOT_MASK;
  742. i2s->rx_mask = DEFAULT_I2S_SLOT_MASK;
  743. i2s->loopback = false;
  744. dev_set_drvdata(dev, i2s);
  745. i2s->clk_i2s = devm_clk_get(dev, "i2s");
  746. if (IS_ERR(i2s->clk_i2s)) {
  747. dev_err(dev, "can't retrieve I2S bit clock\n");
  748. return PTR_ERR(i2s->clk_i2s);
  749. }
  750. /*
  751. * Not an error, as this clock is needed only when some other I/O
  752. * requires input clock from current I2S instance, which is
  753. * configurable from DT.
  754. */
  755. i2s->clk_sync_input = devm_clk_get(dev, "sync_input");
  756. if (IS_ERR(i2s->clk_sync_input))
  757. dev_dbg(dev, "can't retrieve I2S sync input clock\n");
  758. regs = devm_platform_ioremap_resource(pdev, 0);
  759. if (IS_ERR(regs))
  760. return PTR_ERR(regs);
  761. i2s->regmap = devm_regmap_init_mmio(dev, regs,
  762. &tegra210_i2s_regmap_config);
  763. if (IS_ERR(i2s->regmap)) {
  764. dev_err(dev, "regmap init failed\n");
  765. return PTR_ERR(i2s->regmap);
  766. }
  767. regcache_cache_only(i2s->regmap, true);
  768. err = devm_snd_soc_register_component(dev, &tegra210_i2s_cmpnt,
  769. tegra210_i2s_dais,
  770. ARRAY_SIZE(tegra210_i2s_dais));
  771. if (err) {
  772. dev_err(dev, "can't register I2S component, err: %d\n", err);
  773. return err;
  774. }
  775. pm_runtime_enable(dev);
  776. return 0;
  777. }
  778. static int tegra210_i2s_remove(struct platform_device *pdev)
  779. {
  780. pm_runtime_disable(&pdev->dev);
  781. return 0;
  782. }
  783. static const struct dev_pm_ops tegra210_i2s_pm_ops = {
  784. SET_RUNTIME_PM_OPS(tegra210_i2s_runtime_suspend,
  785. tegra210_i2s_runtime_resume, NULL)
  786. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  787. pm_runtime_force_resume)
  788. };
  789. static const struct of_device_id tegra210_i2s_of_match[] = {
  790. { .compatible = "nvidia,tegra210-i2s" },
  791. {},
  792. };
  793. MODULE_DEVICE_TABLE(of, tegra210_i2s_of_match);
  794. static struct platform_driver tegra210_i2s_driver = {
  795. .driver = {
  796. .name = "tegra210-i2s",
  797. .of_match_table = tegra210_i2s_of_match,
  798. .pm = &tegra210_i2s_pm_ops,
  799. },
  800. .probe = tegra210_i2s_probe,
  801. .remove = tegra210_i2s_remove,
  802. };
  803. module_platform_driver(tegra210_i2s_driver)
  804. MODULE_AUTHOR("Songhee Baek <[email protected]>");
  805. MODULE_DESCRIPTION("Tegra210 ASoC I2S driver");
  806. MODULE_LICENSE("GPL v2");