tegra210_mvc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // tegra210_mvc.c - Tegra210 MVC driver
  4. //
  5. // Copyright (c) 2021 NVIDIA CORPORATION. All rights reserved.
  6. #include <linux/clk.h>
  7. #include <linux/device.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/regmap.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include "tegra210_mvc.h"
  20. #include "tegra_cif.h"
  21. static const struct reg_default tegra210_mvc_reg_defaults[] = {
  22. { TEGRA210_MVC_RX_INT_MASK, 0x00000001},
  23. { TEGRA210_MVC_RX_CIF_CTRL, 0x00007700},
  24. { TEGRA210_MVC_TX_INT_MASK, 0x00000001},
  25. { TEGRA210_MVC_TX_CIF_CTRL, 0x00007700},
  26. { TEGRA210_MVC_CG, 0x1},
  27. { TEGRA210_MVC_CTRL, TEGRA210_MVC_CTRL_DEFAULT},
  28. { TEGRA210_MVC_INIT_VOL, 0x00800000},
  29. { TEGRA210_MVC_TARGET_VOL, 0x00800000},
  30. { TEGRA210_MVC_DURATION, 0x000012c0},
  31. { TEGRA210_MVC_DURATION_INV, 0x0006d3a0},
  32. { TEGRA210_MVC_POLY_N1, 0x0000007d},
  33. { TEGRA210_MVC_POLY_N2, 0x00000271},
  34. { TEGRA210_MVC_PEAK_CTRL, 0x000012c0},
  35. { TEGRA210_MVC_CFG_RAM_CTRL, 0x00004000},
  36. };
  37. static const struct tegra210_mvc_gain_params gain_params = {
  38. .poly_coeff = { 23738319, 659403, -3680,
  39. 15546680, 2530732, -120985,
  40. 12048422, 5527252, -785042 },
  41. .poly_n1 = 16,
  42. .poly_n2 = 63,
  43. .duration = 150,
  44. .duration_inv = 14316558,
  45. };
  46. static int __maybe_unused tegra210_mvc_runtime_suspend(struct device *dev)
  47. {
  48. struct tegra210_mvc *mvc = dev_get_drvdata(dev);
  49. regmap_read(mvc->regmap, TEGRA210_MVC_CTRL, &(mvc->ctrl_value));
  50. regcache_cache_only(mvc->regmap, true);
  51. regcache_mark_dirty(mvc->regmap);
  52. return 0;
  53. }
  54. static int __maybe_unused tegra210_mvc_runtime_resume(struct device *dev)
  55. {
  56. struct tegra210_mvc *mvc = dev_get_drvdata(dev);
  57. regcache_cache_only(mvc->regmap, false);
  58. regcache_sync(mvc->regmap);
  59. regmap_write(mvc->regmap, TEGRA210_MVC_CTRL, mvc->ctrl_value);
  60. regmap_update_bits(mvc->regmap,
  61. TEGRA210_MVC_SWITCH,
  62. TEGRA210_MVC_VOLUME_SWITCH_MASK,
  63. TEGRA210_MVC_VOLUME_SWITCH_TRIGGER);
  64. return 0;
  65. }
  66. static void tegra210_mvc_write_ram(struct regmap *regmap)
  67. {
  68. int i;
  69. regmap_write(regmap, TEGRA210_MVC_CFG_RAM_CTRL,
  70. TEGRA210_MVC_CFG_RAM_CTRL_SEQ_ACCESS_EN |
  71. TEGRA210_MVC_CFG_RAM_CTRL_ADDR_INIT_EN |
  72. TEGRA210_MVC_CFG_RAM_CTRL_RW_WRITE);
  73. for (i = 0; i < NUM_GAIN_POLY_COEFFS; i++)
  74. regmap_write(regmap, TEGRA210_MVC_CFG_RAM_DATA,
  75. gain_params.poly_coeff[i]);
  76. }
  77. static void tegra210_mvc_conv_vol(struct tegra210_mvc *mvc, u8 chan, s32 val)
  78. {
  79. /*
  80. * Volume control read from mixer control is with
  81. * 100x scaling; for CURVE_POLY the reg range
  82. * is 0-100 (linear, Q24) and for CURVE_LINEAR
  83. * it is -120dB to +40dB (Q8)
  84. */
  85. if (mvc->curve_type == CURVE_POLY) {
  86. if (val > 10000)
  87. val = 10000;
  88. mvc->volume[chan] = ((val * (1<<8)) / 100) << 16;
  89. } else {
  90. val -= 12000;
  91. mvc->volume[chan] = (val * (1<<8)) / 100;
  92. }
  93. }
  94. static u32 tegra210_mvc_get_ctrl_reg(struct snd_kcontrol *kcontrol)
  95. {
  96. struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
  97. struct tegra210_mvc *mvc = snd_soc_component_get_drvdata(cmpnt);
  98. u32 val;
  99. pm_runtime_get_sync(cmpnt->dev);
  100. regmap_read(mvc->regmap, TEGRA210_MVC_CTRL, &val);
  101. pm_runtime_put(cmpnt->dev);
  102. return val;
  103. }
  104. static int tegra210_mvc_get_mute(struct snd_kcontrol *kcontrol,
  105. struct snd_ctl_elem_value *ucontrol)
  106. {
  107. u32 val = tegra210_mvc_get_ctrl_reg(kcontrol);
  108. u8 mute_mask = TEGRA210_GET_MUTE_VAL(val);
  109. /*
  110. * If per channel control is enabled, then return
  111. * exact mute/unmute setting of all channels.
  112. *
  113. * Else report setting based on CH0 bit to reflect
  114. * the correct HW state.
  115. */
  116. if (val & TEGRA210_MVC_PER_CHAN_CTRL_EN) {
  117. ucontrol->value.integer.value[0] = mute_mask;
  118. } else {
  119. if (mute_mask & TEGRA210_MVC_CH0_MUTE_EN)
  120. ucontrol->value.integer.value[0] =
  121. TEGRA210_MUTE_MASK_EN;
  122. else
  123. ucontrol->value.integer.value[0] = 0;
  124. }
  125. return 0;
  126. }
  127. static int tegra210_mvc_get_master_mute(struct snd_kcontrol *kcontrol,
  128. struct snd_ctl_elem_value *ucontrol)
  129. {
  130. u32 val = tegra210_mvc_get_ctrl_reg(kcontrol);
  131. u8 mute_mask = TEGRA210_GET_MUTE_VAL(val);
  132. /*
  133. * If per channel control is disabled, then return
  134. * master mute/unmute setting based on CH0 bit.
  135. *
  136. * Else report settings based on state of all
  137. * channels.
  138. */
  139. if (!(val & TEGRA210_MVC_PER_CHAN_CTRL_EN)) {
  140. ucontrol->value.integer.value[0] =
  141. mute_mask & TEGRA210_MVC_CH0_MUTE_EN;
  142. } else {
  143. if (mute_mask == TEGRA210_MUTE_MASK_EN)
  144. ucontrol->value.integer.value[0] =
  145. TEGRA210_MVC_CH0_MUTE_EN;
  146. else
  147. ucontrol->value.integer.value[0] = 0;
  148. }
  149. return 0;
  150. }
  151. static int tegra210_mvc_volume_switch_timeout(struct snd_soc_component *cmpnt)
  152. {
  153. struct tegra210_mvc *mvc = snd_soc_component_get_drvdata(cmpnt);
  154. u32 value;
  155. int err;
  156. err = regmap_read_poll_timeout(mvc->regmap, TEGRA210_MVC_SWITCH,
  157. value, !(value & TEGRA210_MVC_VOLUME_SWITCH_MASK),
  158. 10, 10000);
  159. if (err < 0)
  160. dev_err(cmpnt->dev,
  161. "Volume switch trigger is still active, err = %d\n",
  162. err);
  163. return err;
  164. }
  165. static int tegra210_mvc_update_mute(struct snd_kcontrol *kcontrol,
  166. struct snd_ctl_elem_value *ucontrol,
  167. bool per_chan_ctrl)
  168. {
  169. struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
  170. struct tegra210_mvc *mvc = snd_soc_component_get_drvdata(cmpnt);
  171. u32 mute_val = ucontrol->value.integer.value[0];
  172. u32 per_ch_ctrl_val;
  173. bool change = false;
  174. int err;
  175. pm_runtime_get_sync(cmpnt->dev);
  176. err = tegra210_mvc_volume_switch_timeout(cmpnt);
  177. if (err < 0)
  178. goto end;
  179. if (per_chan_ctrl) {
  180. per_ch_ctrl_val = TEGRA210_MVC_PER_CHAN_CTRL_EN;
  181. } else {
  182. per_ch_ctrl_val = 0;
  183. if (mute_val)
  184. mute_val = TEGRA210_MUTE_MASK_EN;
  185. }
  186. regmap_update_bits_check(mvc->regmap, TEGRA210_MVC_CTRL,
  187. TEGRA210_MVC_MUTE_MASK,
  188. mute_val << TEGRA210_MVC_MUTE_SHIFT,
  189. &change);
  190. if (change) {
  191. regmap_update_bits(mvc->regmap, TEGRA210_MVC_CTRL,
  192. TEGRA210_MVC_PER_CHAN_CTRL_EN_MASK,
  193. per_ch_ctrl_val);
  194. regmap_update_bits(mvc->regmap, TEGRA210_MVC_SWITCH,
  195. TEGRA210_MVC_VOLUME_SWITCH_MASK,
  196. TEGRA210_MVC_VOLUME_SWITCH_TRIGGER);
  197. }
  198. end:
  199. pm_runtime_put(cmpnt->dev);
  200. if (err < 0)
  201. return err;
  202. if (change)
  203. return 1;
  204. return 0;
  205. }
  206. static int tegra210_mvc_put_mute(struct snd_kcontrol *kcontrol,
  207. struct snd_ctl_elem_value *ucontrol)
  208. {
  209. return tegra210_mvc_update_mute(kcontrol, ucontrol, true);
  210. }
  211. static int tegra210_mvc_put_master_mute(struct snd_kcontrol *kcontrol,
  212. struct snd_ctl_elem_value *ucontrol)
  213. {
  214. return tegra210_mvc_update_mute(kcontrol, ucontrol, false);
  215. }
  216. static int tegra210_mvc_get_vol(struct snd_kcontrol *kcontrol,
  217. struct snd_ctl_elem_value *ucontrol)
  218. {
  219. struct soc_mixer_control *mc =
  220. (struct soc_mixer_control *)kcontrol->private_value;
  221. struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
  222. struct tegra210_mvc *mvc = snd_soc_component_get_drvdata(cmpnt);
  223. u8 chan = TEGRA210_MVC_GET_CHAN(mc->reg, TEGRA210_MVC_TARGET_VOL);
  224. s32 val = mvc->volume[chan];
  225. if (mvc->curve_type == CURVE_POLY) {
  226. val = ((val >> 16) * 100) >> 8;
  227. } else {
  228. val = (val * 100) >> 8;
  229. val += 12000;
  230. }
  231. ucontrol->value.integer.value[0] = val;
  232. return 0;
  233. }
  234. static int tegra210_mvc_get_master_vol(struct snd_kcontrol *kcontrol,
  235. struct snd_ctl_elem_value *ucontrol)
  236. {
  237. return tegra210_mvc_get_vol(kcontrol, ucontrol);
  238. }
  239. static int tegra210_mvc_update_vol(struct snd_kcontrol *kcontrol,
  240. struct snd_ctl_elem_value *ucontrol,
  241. bool per_ch_enable)
  242. {
  243. struct soc_mixer_control *mc =
  244. (struct soc_mixer_control *)kcontrol->private_value;
  245. struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
  246. struct tegra210_mvc *mvc = snd_soc_component_get_drvdata(cmpnt);
  247. u8 chan = TEGRA210_MVC_GET_CHAN(mc->reg, TEGRA210_MVC_TARGET_VOL);
  248. int old_volume = mvc->volume[chan];
  249. int err, i;
  250. pm_runtime_get_sync(cmpnt->dev);
  251. err = tegra210_mvc_volume_switch_timeout(cmpnt);
  252. if (err < 0)
  253. goto end;
  254. tegra210_mvc_conv_vol(mvc, chan, ucontrol->value.integer.value[0]);
  255. if (mvc->volume[chan] == old_volume) {
  256. err = 0;
  257. goto end;
  258. }
  259. if (per_ch_enable) {
  260. regmap_update_bits(mvc->regmap, TEGRA210_MVC_CTRL,
  261. TEGRA210_MVC_PER_CHAN_CTRL_EN_MASK,
  262. TEGRA210_MVC_PER_CHAN_CTRL_EN);
  263. } else {
  264. regmap_update_bits(mvc->regmap, TEGRA210_MVC_CTRL,
  265. TEGRA210_MVC_PER_CHAN_CTRL_EN_MASK, 0);
  266. for (i = 1; i < TEGRA210_MVC_MAX_CHAN_COUNT; i++)
  267. mvc->volume[i] = mvc->volume[chan];
  268. }
  269. /* Configure init volume same as target volume */
  270. regmap_write(mvc->regmap,
  271. TEGRA210_MVC_REG_OFFSET(TEGRA210_MVC_INIT_VOL, chan),
  272. mvc->volume[chan]);
  273. regmap_write(mvc->regmap, mc->reg, mvc->volume[chan]);
  274. regmap_update_bits(mvc->regmap, TEGRA210_MVC_SWITCH,
  275. TEGRA210_MVC_VOLUME_SWITCH_MASK,
  276. TEGRA210_MVC_VOLUME_SWITCH_TRIGGER);
  277. err = 1;
  278. end:
  279. pm_runtime_put(cmpnt->dev);
  280. return err;
  281. }
  282. static int tegra210_mvc_put_vol(struct snd_kcontrol *kcontrol,
  283. struct snd_ctl_elem_value *ucontrol)
  284. {
  285. return tegra210_mvc_update_vol(kcontrol, ucontrol, true);
  286. }
  287. static int tegra210_mvc_put_master_vol(struct snd_kcontrol *kcontrol,
  288. struct snd_ctl_elem_value *ucontrol)
  289. {
  290. return tegra210_mvc_update_vol(kcontrol, ucontrol, false);
  291. }
  292. static void tegra210_mvc_reset_vol_settings(struct tegra210_mvc *mvc,
  293. struct device *dev)
  294. {
  295. int i;
  296. /* Change volume to default init for new curve type */
  297. if (mvc->curve_type == CURVE_POLY) {
  298. for (i = 0; i < TEGRA210_MVC_MAX_CHAN_COUNT; i++)
  299. mvc->volume[i] = TEGRA210_MVC_INIT_VOL_DEFAULT_POLY;
  300. } else {
  301. for (i = 0; i < TEGRA210_MVC_MAX_CHAN_COUNT; i++)
  302. mvc->volume[i] = TEGRA210_MVC_INIT_VOL_DEFAULT_LINEAR;
  303. }
  304. pm_runtime_get_sync(dev);
  305. /* Program curve type */
  306. regmap_update_bits(mvc->regmap, TEGRA210_MVC_CTRL,
  307. TEGRA210_MVC_CURVE_TYPE_MASK,
  308. mvc->curve_type <<
  309. TEGRA210_MVC_CURVE_TYPE_SHIFT);
  310. /* Init volume for all channels */
  311. for (i = 0; i < TEGRA210_MVC_MAX_CHAN_COUNT; i++) {
  312. regmap_write(mvc->regmap,
  313. TEGRA210_MVC_REG_OFFSET(TEGRA210_MVC_INIT_VOL, i),
  314. mvc->volume[i]);
  315. regmap_write(mvc->regmap,
  316. TEGRA210_MVC_REG_OFFSET(TEGRA210_MVC_TARGET_VOL, i),
  317. mvc->volume[i]);
  318. }
  319. /* Trigger volume switch */
  320. regmap_update_bits(mvc->regmap, TEGRA210_MVC_SWITCH,
  321. TEGRA210_MVC_VOLUME_SWITCH_MASK,
  322. TEGRA210_MVC_VOLUME_SWITCH_TRIGGER);
  323. pm_runtime_put(dev);
  324. }
  325. static int tegra210_mvc_get_curve_type(struct snd_kcontrol *kcontrol,
  326. struct snd_ctl_elem_value *ucontrol)
  327. {
  328. struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
  329. struct tegra210_mvc *mvc = snd_soc_component_get_drvdata(cmpnt);
  330. ucontrol->value.enumerated.item[0] = mvc->curve_type;
  331. return 0;
  332. }
  333. static int tegra210_mvc_put_curve_type(struct snd_kcontrol *kcontrol,
  334. struct snd_ctl_elem_value *ucontrol)
  335. {
  336. struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
  337. struct tegra210_mvc *mvc = snd_soc_component_get_drvdata(cmpnt);
  338. unsigned int value;
  339. regmap_read(mvc->regmap, TEGRA210_MVC_ENABLE, &value);
  340. if (value & TEGRA210_MVC_EN) {
  341. dev_err(cmpnt->dev,
  342. "Curve type can't be set when MVC is running\n");
  343. return -EINVAL;
  344. }
  345. if (mvc->curve_type == ucontrol->value.enumerated.item[0])
  346. return 0;
  347. mvc->curve_type = ucontrol->value.enumerated.item[0];
  348. tegra210_mvc_reset_vol_settings(mvc, cmpnt->dev);
  349. return 1;
  350. }
  351. static int tegra210_mvc_set_audio_cif(struct tegra210_mvc *mvc,
  352. struct snd_pcm_hw_params *params,
  353. unsigned int reg)
  354. {
  355. unsigned int channels, audio_bits;
  356. struct tegra_cif_conf cif_conf;
  357. memset(&cif_conf, 0, sizeof(struct tegra_cif_conf));
  358. channels = params_channels(params);
  359. switch (params_format(params)) {
  360. case SNDRV_PCM_FORMAT_S16_LE:
  361. audio_bits = TEGRA_ACIF_BITS_16;
  362. break;
  363. case SNDRV_PCM_FORMAT_S32_LE:
  364. audio_bits = TEGRA_ACIF_BITS_32;
  365. break;
  366. default:
  367. return -EINVAL;
  368. }
  369. cif_conf.audio_ch = channels;
  370. cif_conf.client_ch = channels;
  371. cif_conf.audio_bits = audio_bits;
  372. cif_conf.client_bits = audio_bits;
  373. tegra_set_cif(mvc->regmap, reg, &cif_conf);
  374. return 0;
  375. }
  376. static int tegra210_mvc_hw_params(struct snd_pcm_substream *substream,
  377. struct snd_pcm_hw_params *params,
  378. struct snd_soc_dai *dai)
  379. {
  380. struct device *dev = dai->dev;
  381. struct tegra210_mvc *mvc = snd_soc_dai_get_drvdata(dai);
  382. int err, val;
  383. /*
  384. * Soft Reset: Below performs module soft reset which clears
  385. * all FSM logic, flushes flow control of FIFO and resets the
  386. * state register. It also brings module back to disabled
  387. * state (without flushing the data in the pipe).
  388. */
  389. regmap_write(mvc->regmap, TEGRA210_MVC_SOFT_RESET, 1);
  390. err = regmap_read_poll_timeout(mvc->regmap, TEGRA210_MVC_SOFT_RESET,
  391. val, !val, 10, 10000);
  392. if (err < 0) {
  393. dev_err(dev, "SW reset failed, err = %d\n", err);
  394. return err;
  395. }
  396. /* Set RX CIF */
  397. err = tegra210_mvc_set_audio_cif(mvc, params, TEGRA210_MVC_RX_CIF_CTRL);
  398. if (err) {
  399. dev_err(dev, "Can't set MVC RX CIF: %d\n", err);
  400. return err;
  401. }
  402. /* Set TX CIF */
  403. err = tegra210_mvc_set_audio_cif(mvc, params, TEGRA210_MVC_TX_CIF_CTRL);
  404. if (err) {
  405. dev_err(dev, "Can't set MVC TX CIF: %d\n", err);
  406. return err;
  407. }
  408. tegra210_mvc_write_ram(mvc->regmap);
  409. /* Program poly_n1, poly_n2, duration */
  410. regmap_write(mvc->regmap, TEGRA210_MVC_POLY_N1, gain_params.poly_n1);
  411. regmap_write(mvc->regmap, TEGRA210_MVC_POLY_N2, gain_params.poly_n2);
  412. regmap_write(mvc->regmap, TEGRA210_MVC_DURATION, gain_params.duration);
  413. /* Program duration_inv */
  414. regmap_write(mvc->regmap, TEGRA210_MVC_DURATION_INV,
  415. gain_params.duration_inv);
  416. return 0;
  417. }
  418. static const struct snd_soc_dai_ops tegra210_mvc_dai_ops = {
  419. .hw_params = tegra210_mvc_hw_params,
  420. };
  421. static const char * const tegra210_mvc_curve_type_text[] = {
  422. "Poly",
  423. "Linear",
  424. };
  425. static const struct soc_enum tegra210_mvc_curve_type_ctrl =
  426. SOC_ENUM_SINGLE_EXT(2, tegra210_mvc_curve_type_text);
  427. #define TEGRA210_MVC_VOL_CTRL(chan) \
  428. SOC_SINGLE_EXT("Channel" #chan " Volume", \
  429. TEGRA210_MVC_REG_OFFSET(TEGRA210_MVC_TARGET_VOL, \
  430. (chan - 1)), \
  431. 0, 16000, 0, tegra210_mvc_get_vol, \
  432. tegra210_mvc_put_vol)
  433. static const struct snd_kcontrol_new tegra210_mvc_vol_ctrl[] = {
  434. /* Per channel volume control */
  435. TEGRA210_MVC_VOL_CTRL(1),
  436. TEGRA210_MVC_VOL_CTRL(2),
  437. TEGRA210_MVC_VOL_CTRL(3),
  438. TEGRA210_MVC_VOL_CTRL(4),
  439. TEGRA210_MVC_VOL_CTRL(5),
  440. TEGRA210_MVC_VOL_CTRL(6),
  441. TEGRA210_MVC_VOL_CTRL(7),
  442. TEGRA210_MVC_VOL_CTRL(8),
  443. /* Per channel mute */
  444. SOC_SINGLE_EXT("Per Chan Mute Mask",
  445. TEGRA210_MVC_CTRL, 0, TEGRA210_MUTE_MASK_EN, 0,
  446. tegra210_mvc_get_mute, tegra210_mvc_put_mute),
  447. /* Master volume */
  448. SOC_SINGLE_EXT("Volume", TEGRA210_MVC_TARGET_VOL, 0, 16000, 0,
  449. tegra210_mvc_get_master_vol,
  450. tegra210_mvc_put_master_vol),
  451. /* Master mute */
  452. SOC_SINGLE_EXT("Mute", TEGRA210_MVC_CTRL, 0, 1, 0,
  453. tegra210_mvc_get_master_mute,
  454. tegra210_mvc_put_master_mute),
  455. SOC_ENUM_EXT("Curve Type", tegra210_mvc_curve_type_ctrl,
  456. tegra210_mvc_get_curve_type, tegra210_mvc_put_curve_type),
  457. };
  458. static struct snd_soc_dai_driver tegra210_mvc_dais[] = {
  459. /* Input */
  460. {
  461. .name = "MVC-RX-CIF",
  462. .playback = {
  463. .stream_name = "RX-CIF-Playback",
  464. .channels_min = 1,
  465. .channels_max = 8,
  466. .rates = SNDRV_PCM_RATE_8000_192000,
  467. .formats = SNDRV_PCM_FMTBIT_S8 |
  468. SNDRV_PCM_FMTBIT_S16_LE |
  469. SNDRV_PCM_FMTBIT_S32_LE,
  470. },
  471. .capture = {
  472. .stream_name = "RX-CIF-Capture",
  473. .channels_min = 1,
  474. .channels_max = 8,
  475. .rates = SNDRV_PCM_RATE_8000_192000,
  476. .formats = SNDRV_PCM_FMTBIT_S8 |
  477. SNDRV_PCM_FMTBIT_S16_LE |
  478. SNDRV_PCM_FMTBIT_S32_LE,
  479. },
  480. },
  481. /* Output */
  482. {
  483. .name = "MVC-TX-CIF",
  484. .playback = {
  485. .stream_name = "TX-CIF-Playback",
  486. .channels_min = 1,
  487. .channels_max = 8,
  488. .rates = SNDRV_PCM_RATE_8000_192000,
  489. .formats = SNDRV_PCM_FMTBIT_S8 |
  490. SNDRV_PCM_FMTBIT_S16_LE |
  491. SNDRV_PCM_FMTBIT_S32_LE,
  492. },
  493. .capture = {
  494. .stream_name = "TX-CIF-Capture",
  495. .channels_min = 1,
  496. .channels_max = 8,
  497. .rates = SNDRV_PCM_RATE_8000_192000,
  498. .formats = SNDRV_PCM_FMTBIT_S8 |
  499. SNDRV_PCM_FMTBIT_S16_LE |
  500. SNDRV_PCM_FMTBIT_S32_LE,
  501. },
  502. .ops = &tegra210_mvc_dai_ops,
  503. }
  504. };
  505. static const struct snd_soc_dapm_widget tegra210_mvc_widgets[] = {
  506. SND_SOC_DAPM_AIF_IN("RX", NULL, 0, SND_SOC_NOPM, 0, 0),
  507. SND_SOC_DAPM_AIF_OUT("TX", NULL, 0, TEGRA210_MVC_ENABLE,
  508. TEGRA210_MVC_EN_SHIFT, 0),
  509. };
  510. #define MVC_ROUTES(sname) \
  511. { "RX XBAR-" sname, NULL, "XBAR-TX" }, \
  512. { "RX-CIF-" sname, NULL, "RX XBAR-" sname }, \
  513. { "RX", NULL, "RX-CIF-" sname }, \
  514. { "TX-CIF-" sname, NULL, "TX" }, \
  515. { "TX XBAR-" sname, NULL, "TX-CIF-" sname }, \
  516. { "XBAR-RX", NULL, "TX XBAR-" sname }
  517. static const struct snd_soc_dapm_route tegra210_mvc_routes[] = {
  518. { "TX", NULL, "RX" },
  519. MVC_ROUTES("Playback"),
  520. MVC_ROUTES("Capture"),
  521. };
  522. static const struct snd_soc_component_driver tegra210_mvc_cmpnt = {
  523. .dapm_widgets = tegra210_mvc_widgets,
  524. .num_dapm_widgets = ARRAY_SIZE(tegra210_mvc_widgets),
  525. .dapm_routes = tegra210_mvc_routes,
  526. .num_dapm_routes = ARRAY_SIZE(tegra210_mvc_routes),
  527. .controls = tegra210_mvc_vol_ctrl,
  528. .num_controls = ARRAY_SIZE(tegra210_mvc_vol_ctrl),
  529. };
  530. static bool tegra210_mvc_rd_reg(struct device *dev, unsigned int reg)
  531. {
  532. switch (reg) {
  533. case TEGRA210_MVC_RX_STATUS ... TEGRA210_MVC_CONFIG_ERR_TYPE:
  534. return true;
  535. default:
  536. return false;
  537. };
  538. }
  539. static bool tegra210_mvc_wr_reg(struct device *dev, unsigned int reg)
  540. {
  541. switch (reg) {
  542. case TEGRA210_MVC_RX_INT_MASK ... TEGRA210_MVC_RX_CIF_CTRL:
  543. case TEGRA210_MVC_TX_INT_MASK ... TEGRA210_MVC_TX_CIF_CTRL:
  544. case TEGRA210_MVC_ENABLE ... TEGRA210_MVC_CG:
  545. case TEGRA210_MVC_CTRL ... TEGRA210_MVC_CFG_RAM_DATA:
  546. return true;
  547. default:
  548. return false;
  549. }
  550. }
  551. static bool tegra210_mvc_volatile_reg(struct device *dev, unsigned int reg)
  552. {
  553. switch (reg) {
  554. case TEGRA210_MVC_RX_STATUS:
  555. case TEGRA210_MVC_RX_INT_STATUS:
  556. case TEGRA210_MVC_RX_INT_SET:
  557. case TEGRA210_MVC_TX_STATUS:
  558. case TEGRA210_MVC_TX_INT_STATUS:
  559. case TEGRA210_MVC_TX_INT_SET:
  560. case TEGRA210_MVC_SOFT_RESET:
  561. case TEGRA210_MVC_STATUS:
  562. case TEGRA210_MVC_INT_STATUS:
  563. case TEGRA210_MVC_SWITCH:
  564. case TEGRA210_MVC_CFG_RAM_CTRL:
  565. case TEGRA210_MVC_CFG_RAM_DATA:
  566. case TEGRA210_MVC_PEAK_VALUE:
  567. case TEGRA210_MVC_CTRL:
  568. return true;
  569. default:
  570. return false;
  571. }
  572. }
  573. static const struct regmap_config tegra210_mvc_regmap_config = {
  574. .reg_bits = 32,
  575. .reg_stride = 4,
  576. .val_bits = 32,
  577. .max_register = TEGRA210_MVC_CONFIG_ERR_TYPE,
  578. .writeable_reg = tegra210_mvc_wr_reg,
  579. .readable_reg = tegra210_mvc_rd_reg,
  580. .volatile_reg = tegra210_mvc_volatile_reg,
  581. .reg_defaults = tegra210_mvc_reg_defaults,
  582. .num_reg_defaults = ARRAY_SIZE(tegra210_mvc_reg_defaults),
  583. .cache_type = REGCACHE_FLAT,
  584. };
  585. static const struct of_device_id tegra210_mvc_of_match[] = {
  586. { .compatible = "nvidia,tegra210-mvc" },
  587. {},
  588. };
  589. MODULE_DEVICE_TABLE(of, tegra210_mvc_of_match);
  590. static int tegra210_mvc_platform_probe(struct platform_device *pdev)
  591. {
  592. struct device *dev = &pdev->dev;
  593. struct tegra210_mvc *mvc;
  594. void __iomem *regs;
  595. int err;
  596. mvc = devm_kzalloc(dev, sizeof(*mvc), GFP_KERNEL);
  597. if (!mvc)
  598. return -ENOMEM;
  599. dev_set_drvdata(dev, mvc);
  600. mvc->curve_type = CURVE_LINEAR;
  601. mvc->ctrl_value = TEGRA210_MVC_CTRL_DEFAULT;
  602. regs = devm_platform_ioremap_resource(pdev, 0);
  603. if (IS_ERR(regs))
  604. return PTR_ERR(regs);
  605. mvc->regmap = devm_regmap_init_mmio(dev, regs,
  606. &tegra210_mvc_regmap_config);
  607. if (IS_ERR(mvc->regmap)) {
  608. dev_err(dev, "regmap init failed\n");
  609. return PTR_ERR(mvc->regmap);
  610. }
  611. regcache_cache_only(mvc->regmap, true);
  612. err = devm_snd_soc_register_component(dev, &tegra210_mvc_cmpnt,
  613. tegra210_mvc_dais,
  614. ARRAY_SIZE(tegra210_mvc_dais));
  615. if (err) {
  616. dev_err(dev, "can't register MVC component, err: %d\n", err);
  617. return err;
  618. }
  619. pm_runtime_enable(dev);
  620. tegra210_mvc_reset_vol_settings(mvc, &pdev->dev);
  621. return 0;
  622. }
  623. static int tegra210_mvc_platform_remove(struct platform_device *pdev)
  624. {
  625. pm_runtime_disable(&pdev->dev);
  626. return 0;
  627. }
  628. static const struct dev_pm_ops tegra210_mvc_pm_ops = {
  629. SET_RUNTIME_PM_OPS(tegra210_mvc_runtime_suspend,
  630. tegra210_mvc_runtime_resume, NULL)
  631. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  632. pm_runtime_force_resume)
  633. };
  634. static struct platform_driver tegra210_mvc_driver = {
  635. .driver = {
  636. .name = "tegra210-mvc",
  637. .of_match_table = tegra210_mvc_of_match,
  638. .pm = &tegra210_mvc_pm_ops,
  639. },
  640. .probe = tegra210_mvc_platform_probe,
  641. .remove = tegra210_mvc_platform_remove,
  642. };
  643. module_platform_driver(tegra210_mvc_driver)
  644. MODULE_AUTHOR("Arun Shamanna Lakshmi <[email protected]>");
  645. MODULE_DESCRIPTION("Tegra210 MVC ASoC driver");
  646. MODULE_LICENSE("GPL v2");