tegra210_i2s.c 28 KB

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