ux500_msp_dai.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2012
  3. *
  4. * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
  5. * Roger Nilsson <roger.xr.nilsson@stericsson.com>
  6. * for ST-Ericsson.
  7. *
  8. * License terms:
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/bitops.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/clk.h>
  19. #include <linux/of.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/mfd/dbx500-prcmu.h>
  22. #include <linux/platform_data/asoc-ux500-msp.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dai.h>
  25. #include <sound/dmaengine_pcm.h>
  26. #include "ux500_msp_i2s.h"
  27. #include "ux500_msp_dai.h"
  28. #include "ux500_pcm.h"
  29. static int setup_pcm_multichan(struct snd_soc_dai *dai,
  30. struct ux500_msp_config *msp_config)
  31. {
  32. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  33. struct msp_multichannel_config *multi =
  34. &msp_config->multichannel_config;
  35. if (drvdata->slots > 1) {
  36. msp_config->multichannel_configured = 1;
  37. multi->tx_multichannel_enable = true;
  38. multi->rx_multichannel_enable = true;
  39. multi->rx_comparison_enable_mode = MSP_COMPARISON_DISABLED;
  40. multi->tx_channel_0_enable = drvdata->tx_mask;
  41. multi->tx_channel_1_enable = 0;
  42. multi->tx_channel_2_enable = 0;
  43. multi->tx_channel_3_enable = 0;
  44. multi->rx_channel_0_enable = drvdata->rx_mask;
  45. multi->rx_channel_1_enable = 0;
  46. multi->rx_channel_2_enable = 0;
  47. multi->rx_channel_3_enable = 0;
  48. dev_dbg(dai->dev,
  49. "%s: Multichannel enabled. Slots: %d, TX: %u, RX: %u\n",
  50. __func__, drvdata->slots, multi->tx_channel_0_enable,
  51. multi->rx_channel_0_enable);
  52. }
  53. return 0;
  54. }
  55. static int setup_frameper(struct snd_soc_dai *dai, unsigned int rate,
  56. struct msp_protdesc *prot_desc)
  57. {
  58. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  59. switch (drvdata->slots) {
  60. case 1:
  61. switch (rate) {
  62. case 8000:
  63. prot_desc->frame_period =
  64. FRAME_PER_SINGLE_SLOT_8_KHZ;
  65. break;
  66. case 16000:
  67. prot_desc->frame_period =
  68. FRAME_PER_SINGLE_SLOT_16_KHZ;
  69. break;
  70. case 44100:
  71. prot_desc->frame_period =
  72. FRAME_PER_SINGLE_SLOT_44_1_KHZ;
  73. break;
  74. case 48000:
  75. prot_desc->frame_period =
  76. FRAME_PER_SINGLE_SLOT_48_KHZ;
  77. break;
  78. default:
  79. dev_err(dai->dev,
  80. "%s: Error: Unsupported sample-rate (freq = %d)!\n",
  81. __func__, rate);
  82. return -EINVAL;
  83. }
  84. break;
  85. case 2:
  86. prot_desc->frame_period = FRAME_PER_2_SLOTS;
  87. break;
  88. case 8:
  89. prot_desc->frame_period = FRAME_PER_8_SLOTS;
  90. break;
  91. case 16:
  92. prot_desc->frame_period = FRAME_PER_16_SLOTS;
  93. break;
  94. default:
  95. dev_err(dai->dev,
  96. "%s: Error: Unsupported slot-count (slots = %d)!\n",
  97. __func__, drvdata->slots);
  98. return -EINVAL;
  99. }
  100. prot_desc->clocks_per_frame =
  101. prot_desc->frame_period+1;
  102. dev_dbg(dai->dev, "%s: Clocks per frame: %u\n",
  103. __func__,
  104. prot_desc->clocks_per_frame);
  105. return 0;
  106. }
  107. static int setup_pcm_framing(struct snd_soc_dai *dai, unsigned int rate,
  108. struct msp_protdesc *prot_desc)
  109. {
  110. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  111. u32 frame_length = MSP_FRAME_LEN_1;
  112. prot_desc->frame_width = 0;
  113. switch (drvdata->slots) {
  114. case 1:
  115. frame_length = MSP_FRAME_LEN_1;
  116. break;
  117. case 2:
  118. frame_length = MSP_FRAME_LEN_2;
  119. break;
  120. case 8:
  121. frame_length = MSP_FRAME_LEN_8;
  122. break;
  123. case 16:
  124. frame_length = MSP_FRAME_LEN_16;
  125. break;
  126. default:
  127. dev_err(dai->dev,
  128. "%s: Error: Unsupported slot-count (slots = %d)!\n",
  129. __func__, drvdata->slots);
  130. return -EINVAL;
  131. }
  132. prot_desc->tx_frame_len_1 = frame_length;
  133. prot_desc->rx_frame_len_1 = frame_length;
  134. prot_desc->tx_frame_len_2 = frame_length;
  135. prot_desc->rx_frame_len_2 = frame_length;
  136. prot_desc->tx_elem_len_1 = MSP_ELEM_LEN_16;
  137. prot_desc->rx_elem_len_1 = MSP_ELEM_LEN_16;
  138. prot_desc->tx_elem_len_2 = MSP_ELEM_LEN_16;
  139. prot_desc->rx_elem_len_2 = MSP_ELEM_LEN_16;
  140. return setup_frameper(dai, rate, prot_desc);
  141. }
  142. static int setup_clocking(struct snd_soc_dai *dai,
  143. unsigned int fmt,
  144. struct ux500_msp_config *msp_config)
  145. {
  146. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  147. case SND_SOC_DAIFMT_NB_NF:
  148. break;
  149. case SND_SOC_DAIFMT_NB_IF:
  150. msp_config->tx_fsync_pol ^= 1 << TFSPOL_SHIFT;
  151. msp_config->rx_fsync_pol ^= 1 << RFSPOL_SHIFT;
  152. break;
  153. default:
  154. dev_err(dai->dev,
  155. "%s: Error: Unsupported inversion (fmt = 0x%x)!\n",
  156. __func__, fmt);
  157. return -EINVAL;
  158. }
  159. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  160. case SND_SOC_DAIFMT_CBM_CFM:
  161. dev_dbg(dai->dev, "%s: Codec is master.\n", __func__);
  162. msp_config->iodelay = 0x20;
  163. msp_config->rx_fsync_sel = 0;
  164. msp_config->tx_fsync_sel = 1 << TFSSEL_SHIFT;
  165. msp_config->tx_clk_sel = 0;
  166. msp_config->rx_clk_sel = 0;
  167. msp_config->srg_clk_sel = 0x2 << SCKSEL_SHIFT;
  168. break;
  169. case SND_SOC_DAIFMT_CBS_CFS:
  170. dev_dbg(dai->dev, "%s: Codec is slave.\n", __func__);
  171. msp_config->tx_clk_sel = TX_CLK_SEL_SRG;
  172. msp_config->tx_fsync_sel = TX_SYNC_SRG_PROG;
  173. msp_config->rx_clk_sel = RX_CLK_SEL_SRG;
  174. msp_config->rx_fsync_sel = RX_SYNC_SRG;
  175. msp_config->srg_clk_sel = 1 << SCKSEL_SHIFT;
  176. break;
  177. default:
  178. dev_err(dai->dev, "%s: Error: Unsupported master (fmt = 0x%x)!\n",
  179. __func__, fmt);
  180. return -EINVAL;
  181. }
  182. return 0;
  183. }
  184. static int setup_pcm_protdesc(struct snd_soc_dai *dai,
  185. unsigned int fmt,
  186. struct msp_protdesc *prot_desc)
  187. {
  188. prot_desc->rx_phase_mode = MSP_SINGLE_PHASE;
  189. prot_desc->tx_phase_mode = MSP_SINGLE_PHASE;
  190. prot_desc->rx_phase2_start_mode = MSP_PHASE2_START_MODE_IMEDIATE;
  191. prot_desc->tx_phase2_start_mode = MSP_PHASE2_START_MODE_IMEDIATE;
  192. prot_desc->rx_byte_order = MSP_BTF_MS_BIT_FIRST;
  193. prot_desc->tx_byte_order = MSP_BTF_MS_BIT_FIRST;
  194. prot_desc->tx_fsync_pol = MSP_FSYNC_POL(MSP_FSYNC_POL_ACT_HI);
  195. prot_desc->rx_fsync_pol = MSP_FSYNC_POL_ACT_HI << RFSPOL_SHIFT;
  196. if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_DSP_A) {
  197. dev_dbg(dai->dev, "%s: DSP_A.\n", __func__);
  198. prot_desc->rx_clk_pol = MSP_RISING_EDGE;
  199. prot_desc->tx_clk_pol = MSP_FALLING_EDGE;
  200. prot_desc->rx_data_delay = MSP_DELAY_1;
  201. prot_desc->tx_data_delay = MSP_DELAY_1;
  202. } else {
  203. dev_dbg(dai->dev, "%s: DSP_B.\n", __func__);
  204. prot_desc->rx_clk_pol = MSP_FALLING_EDGE;
  205. prot_desc->tx_clk_pol = MSP_RISING_EDGE;
  206. prot_desc->rx_data_delay = MSP_DELAY_0;
  207. prot_desc->tx_data_delay = MSP_DELAY_0;
  208. }
  209. prot_desc->rx_half_word_swap = MSP_SWAP_NONE;
  210. prot_desc->tx_half_word_swap = MSP_SWAP_NONE;
  211. prot_desc->compression_mode = MSP_COMPRESS_MODE_LINEAR;
  212. prot_desc->expansion_mode = MSP_EXPAND_MODE_LINEAR;
  213. prot_desc->frame_sync_ignore = MSP_FSYNC_IGNORE;
  214. return 0;
  215. }
  216. static int setup_i2s_protdesc(struct msp_protdesc *prot_desc)
  217. {
  218. prot_desc->rx_phase_mode = MSP_DUAL_PHASE;
  219. prot_desc->tx_phase_mode = MSP_DUAL_PHASE;
  220. prot_desc->rx_phase2_start_mode = MSP_PHASE2_START_MODE_FSYNC;
  221. prot_desc->tx_phase2_start_mode = MSP_PHASE2_START_MODE_FSYNC;
  222. prot_desc->rx_byte_order = MSP_BTF_MS_BIT_FIRST;
  223. prot_desc->tx_byte_order = MSP_BTF_MS_BIT_FIRST;
  224. prot_desc->tx_fsync_pol = MSP_FSYNC_POL(MSP_FSYNC_POL_ACT_LO);
  225. prot_desc->rx_fsync_pol = MSP_FSYNC_POL_ACT_LO << RFSPOL_SHIFT;
  226. prot_desc->rx_frame_len_1 = MSP_FRAME_LEN_1;
  227. prot_desc->rx_frame_len_2 = MSP_FRAME_LEN_1;
  228. prot_desc->tx_frame_len_1 = MSP_FRAME_LEN_1;
  229. prot_desc->tx_frame_len_2 = MSP_FRAME_LEN_1;
  230. prot_desc->rx_elem_len_1 = MSP_ELEM_LEN_16;
  231. prot_desc->rx_elem_len_2 = MSP_ELEM_LEN_16;
  232. prot_desc->tx_elem_len_1 = MSP_ELEM_LEN_16;
  233. prot_desc->tx_elem_len_2 = MSP_ELEM_LEN_16;
  234. prot_desc->rx_clk_pol = MSP_RISING_EDGE;
  235. prot_desc->tx_clk_pol = MSP_FALLING_EDGE;
  236. prot_desc->rx_data_delay = MSP_DELAY_0;
  237. prot_desc->tx_data_delay = MSP_DELAY_0;
  238. prot_desc->tx_half_word_swap = MSP_SWAP_NONE;
  239. prot_desc->rx_half_word_swap = MSP_SWAP_NONE;
  240. prot_desc->compression_mode = MSP_COMPRESS_MODE_LINEAR;
  241. prot_desc->expansion_mode = MSP_EXPAND_MODE_LINEAR;
  242. prot_desc->frame_sync_ignore = MSP_FSYNC_IGNORE;
  243. return 0;
  244. }
  245. static int setup_msp_config(struct snd_pcm_substream *substream,
  246. struct snd_soc_dai *dai,
  247. struct ux500_msp_config *msp_config)
  248. {
  249. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  250. struct msp_protdesc *prot_desc = &msp_config->protdesc;
  251. struct snd_pcm_runtime *runtime = substream->runtime;
  252. unsigned int fmt = drvdata->fmt;
  253. int ret;
  254. memset(msp_config, 0, sizeof(*msp_config));
  255. msp_config->f_inputclk = drvdata->master_clk;
  256. msp_config->tx_fifo_config = TX_FIFO_ENABLE;
  257. msp_config->rx_fifo_config = RX_FIFO_ENABLE;
  258. msp_config->def_elem_len = 1;
  259. msp_config->direction = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  260. MSP_DIR_TX : MSP_DIR_RX;
  261. msp_config->data_size = MSP_DATA_BITS_32;
  262. msp_config->frame_freq = runtime->rate;
  263. dev_dbg(dai->dev, "%s: f_inputclk = %u, frame_freq = %u.\n",
  264. __func__, msp_config->f_inputclk, msp_config->frame_freq);
  265. /* To avoid division by zero */
  266. prot_desc->clocks_per_frame = 1;
  267. dev_dbg(dai->dev, "%s: rate: %u, channels: %d.\n", __func__,
  268. runtime->rate, runtime->channels);
  269. switch (fmt &
  270. (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
  271. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
  272. dev_dbg(dai->dev, "%s: SND_SOC_DAIFMT_I2S.\n", __func__);
  273. msp_config->default_protdesc = 1;
  274. msp_config->protocol = MSP_I2S_PROTOCOL;
  275. break;
  276. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
  277. dev_dbg(dai->dev, "%s: SND_SOC_DAIFMT_I2S.\n", __func__);
  278. msp_config->data_size = MSP_DATA_BITS_16;
  279. msp_config->protocol = MSP_I2S_PROTOCOL;
  280. ret = setup_i2s_protdesc(prot_desc);
  281. if (ret < 0)
  282. return ret;
  283. break;
  284. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
  285. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
  286. case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBS_CFS:
  287. case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM:
  288. dev_dbg(dai->dev, "%s: PCM format.\n", __func__);
  289. msp_config->data_size = MSP_DATA_BITS_16;
  290. msp_config->protocol = MSP_PCM_PROTOCOL;
  291. ret = setup_pcm_protdesc(dai, fmt, prot_desc);
  292. if (ret < 0)
  293. return ret;
  294. ret = setup_pcm_multichan(dai, msp_config);
  295. if (ret < 0)
  296. return ret;
  297. ret = setup_pcm_framing(dai, runtime->rate, prot_desc);
  298. if (ret < 0)
  299. return ret;
  300. break;
  301. default:
  302. dev_err(dai->dev, "%s: Error: Unsupported format (%d)!\n",
  303. __func__, fmt);
  304. return -EINVAL;
  305. }
  306. return setup_clocking(dai, fmt, msp_config);
  307. }
  308. static int ux500_msp_dai_startup(struct snd_pcm_substream *substream,
  309. struct snd_soc_dai *dai)
  310. {
  311. int ret = 0;
  312. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  313. dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
  314. snd_pcm_stream_str(substream));
  315. /* Enable regulator */
  316. ret = regulator_enable(drvdata->reg_vape);
  317. if (ret != 0) {
  318. dev_err(drvdata->msp->dev,
  319. "%s: Failed to enable regulator!\n", __func__);
  320. return ret;
  321. }
  322. /* Prepare and enable clocks */
  323. dev_dbg(dai->dev, "%s: Enabling MSP-clocks.\n", __func__);
  324. ret = clk_prepare_enable(drvdata->pclk);
  325. if (ret) {
  326. dev_err(drvdata->msp->dev,
  327. "%s: Failed to prepare/enable pclk!\n", __func__);
  328. goto err_pclk;
  329. }
  330. ret = clk_prepare_enable(drvdata->clk);
  331. if (ret) {
  332. dev_err(drvdata->msp->dev,
  333. "%s: Failed to prepare/enable clk!\n", __func__);
  334. goto err_clk;
  335. }
  336. return ret;
  337. err_clk:
  338. clk_disable_unprepare(drvdata->pclk);
  339. err_pclk:
  340. regulator_disable(drvdata->reg_vape);
  341. return ret;
  342. }
  343. static void ux500_msp_dai_shutdown(struct snd_pcm_substream *substream,
  344. struct snd_soc_dai *dai)
  345. {
  346. int ret;
  347. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  348. bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  349. dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
  350. snd_pcm_stream_str(substream));
  351. if (drvdata->vape_opp_constraint == 1) {
  352. prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
  353. "ux500_msp_i2s", 50);
  354. drvdata->vape_opp_constraint = 0;
  355. }
  356. if (ux500_msp_i2s_close(drvdata->msp,
  357. is_playback ? MSP_DIR_TX : MSP_DIR_RX)) {
  358. dev_err(dai->dev,
  359. "%s: Error: MSP %d (%s): Unable to close i2s.\n",
  360. __func__, dai->id, snd_pcm_stream_str(substream));
  361. }
  362. /* Disable and unprepare clocks */
  363. clk_disable_unprepare(drvdata->clk);
  364. clk_disable_unprepare(drvdata->pclk);
  365. /* Disable regulator */
  366. ret = regulator_disable(drvdata->reg_vape);
  367. if (ret < 0)
  368. dev_err(dai->dev,
  369. "%s: ERROR: Failed to disable regulator (%d)!\n",
  370. __func__, ret);
  371. }
  372. static int ux500_msp_dai_prepare(struct snd_pcm_substream *substream,
  373. struct snd_soc_dai *dai)
  374. {
  375. int ret = 0;
  376. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  377. struct snd_pcm_runtime *runtime = substream->runtime;
  378. struct ux500_msp_config msp_config;
  379. dev_dbg(dai->dev, "%s: MSP %d (%s): Enter (rate = %d).\n", __func__,
  380. dai->id, snd_pcm_stream_str(substream), runtime->rate);
  381. setup_msp_config(substream, dai, &msp_config);
  382. ret = ux500_msp_i2s_open(drvdata->msp, &msp_config);
  383. if (ret < 0) {
  384. dev_err(dai->dev, "%s: Error: msp_setup failed (ret = %d)!\n",
  385. __func__, ret);
  386. return ret;
  387. }
  388. /* Set OPP-level */
  389. if ((drvdata->fmt & SND_SOC_DAIFMT_MASTER_MASK) &&
  390. (drvdata->msp->f_bitclk > 19200000)) {
  391. /* If the bit-clock is higher than 19.2MHz, Vape should be
  392. * run in 100% OPP. Only when bit-clock is used (MSP master) */
  393. prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
  394. "ux500-msp-i2s", 100);
  395. drvdata->vape_opp_constraint = 1;
  396. } else {
  397. prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
  398. "ux500-msp-i2s", 50);
  399. drvdata->vape_opp_constraint = 0;
  400. }
  401. return ret;
  402. }
  403. static int ux500_msp_dai_hw_params(struct snd_pcm_substream *substream,
  404. struct snd_pcm_hw_params *params,
  405. struct snd_soc_dai *dai)
  406. {
  407. unsigned int mask, slots_active;
  408. struct snd_pcm_runtime *runtime = substream->runtime;
  409. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  410. dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n",
  411. __func__, dai->id, snd_pcm_stream_str(substream));
  412. switch (drvdata->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  413. case SND_SOC_DAIFMT_I2S:
  414. snd_pcm_hw_constraint_minmax(runtime,
  415. SNDRV_PCM_HW_PARAM_CHANNELS,
  416. 1, 2);
  417. break;
  418. case SND_SOC_DAIFMT_DSP_B:
  419. case SND_SOC_DAIFMT_DSP_A:
  420. mask = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  421. drvdata->tx_mask :
  422. drvdata->rx_mask;
  423. slots_active = hweight32(mask);
  424. dev_dbg(dai->dev, "TDM-slots active: %d", slots_active);
  425. snd_pcm_hw_constraint_single(runtime,
  426. SNDRV_PCM_HW_PARAM_CHANNELS,
  427. slots_active);
  428. break;
  429. default:
  430. dev_err(dai->dev,
  431. "%s: Error: Unsupported protocol (fmt = 0x%x)!\n",
  432. __func__, drvdata->fmt);
  433. return -EINVAL;
  434. }
  435. return 0;
  436. }
  437. static int ux500_msp_dai_set_dai_fmt(struct snd_soc_dai *dai,
  438. unsigned int fmt)
  439. {
  440. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  441. dev_dbg(dai->dev, "%s: MSP %d: Enter.\n", __func__, dai->id);
  442. switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
  443. SND_SOC_DAIFMT_MASTER_MASK)) {
  444. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
  445. case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
  446. case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBS_CFS:
  447. case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM:
  448. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
  449. case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
  450. break;
  451. default:
  452. dev_err(dai->dev,
  453. "%s: Error: Unsupported protocol/master (fmt = 0x%x)!\n",
  454. __func__, drvdata->fmt);
  455. return -EINVAL;
  456. }
  457. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  458. case SND_SOC_DAIFMT_NB_NF:
  459. case SND_SOC_DAIFMT_NB_IF:
  460. case SND_SOC_DAIFMT_IB_IF:
  461. break;
  462. default:
  463. dev_err(dai->dev,
  464. "%s: Error: Unsupported inversion (fmt = 0x%x)!\n",
  465. __func__, drvdata->fmt);
  466. return -EINVAL;
  467. }
  468. drvdata->fmt = fmt;
  469. return 0;
  470. }
  471. static int ux500_msp_dai_set_tdm_slot(struct snd_soc_dai *dai,
  472. unsigned int tx_mask,
  473. unsigned int rx_mask,
  474. int slots, int slot_width)
  475. {
  476. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  477. unsigned int cap;
  478. switch (slots) {
  479. case 1:
  480. cap = 0x01;
  481. break;
  482. case 2:
  483. cap = 0x03;
  484. break;
  485. case 8:
  486. cap = 0xFF;
  487. break;
  488. case 16:
  489. cap = 0xFFFF;
  490. break;
  491. default:
  492. dev_err(dai->dev, "%s: Error: Unsupported slot-count (%d)!\n",
  493. __func__, slots);
  494. return -EINVAL;
  495. }
  496. drvdata->slots = slots;
  497. if (!(slot_width == 16)) {
  498. dev_err(dai->dev, "%s: Error: Unsupported slot-width (%d)!\n",
  499. __func__, slot_width);
  500. return -EINVAL;
  501. }
  502. drvdata->slot_width = slot_width;
  503. drvdata->tx_mask = tx_mask & cap;
  504. drvdata->rx_mask = rx_mask & cap;
  505. return 0;
  506. }
  507. static int ux500_msp_dai_set_dai_sysclk(struct snd_soc_dai *dai,
  508. int clk_id, unsigned int freq, int dir)
  509. {
  510. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  511. dev_dbg(dai->dev, "%s: MSP %d: Enter. clk-id: %d, freq: %u.\n",
  512. __func__, dai->id, clk_id, freq);
  513. switch (clk_id) {
  514. case UX500_MSP_MASTER_CLOCK:
  515. drvdata->master_clk = freq;
  516. break;
  517. default:
  518. dev_err(dai->dev, "%s: MSP %d: Invalid clk-id (%d)!\n",
  519. __func__, dai->id, clk_id);
  520. return -EINVAL;
  521. }
  522. return 0;
  523. }
  524. static int ux500_msp_dai_trigger(struct snd_pcm_substream *substream,
  525. int cmd, struct snd_soc_dai *dai)
  526. {
  527. int ret = 0;
  528. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  529. dev_dbg(dai->dev, "%s: MSP %d (%s): Enter (msp->id = %d, cmd = %d).\n",
  530. __func__, dai->id, snd_pcm_stream_str(substream),
  531. (int)drvdata->msp->id, cmd);
  532. ret = ux500_msp_i2s_trigger(drvdata->msp, cmd, substream->stream);
  533. return ret;
  534. }
  535. static int ux500_msp_dai_of_probe(struct snd_soc_dai *dai)
  536. {
  537. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  538. struct snd_dmaengine_dai_dma_data *playback_dma_data;
  539. struct snd_dmaengine_dai_dma_data *capture_dma_data;
  540. playback_dma_data = devm_kzalloc(dai->dev,
  541. sizeof(*playback_dma_data),
  542. GFP_KERNEL);
  543. if (!playback_dma_data)
  544. return -ENOMEM;
  545. capture_dma_data = devm_kzalloc(dai->dev,
  546. sizeof(*capture_dma_data),
  547. GFP_KERNEL);
  548. if (!capture_dma_data)
  549. return -ENOMEM;
  550. playback_dma_data->addr = drvdata->msp->playback_dma_data.tx_rx_addr;
  551. capture_dma_data->addr = drvdata->msp->capture_dma_data.tx_rx_addr;
  552. playback_dma_data->maxburst = 4;
  553. capture_dma_data->maxburst = 4;
  554. snd_soc_dai_init_dma_data(dai, playback_dma_data, capture_dma_data);
  555. return 0;
  556. }
  557. static int ux500_msp_dai_probe(struct snd_soc_dai *dai)
  558. {
  559. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
  560. struct msp_i2s_platform_data *pdata = dai->dev->platform_data;
  561. int ret;
  562. if (!pdata) {
  563. ret = ux500_msp_dai_of_probe(dai);
  564. return ret;
  565. }
  566. drvdata->msp->playback_dma_data.data_size = drvdata->slot_width;
  567. drvdata->msp->capture_dma_data.data_size = drvdata->slot_width;
  568. snd_soc_dai_init_dma_data(dai,
  569. &drvdata->msp->playback_dma_data,
  570. &drvdata->msp->capture_dma_data);
  571. return 0;
  572. }
  573. static struct snd_soc_dai_ops ux500_msp_dai_ops[] = {
  574. {
  575. .set_sysclk = ux500_msp_dai_set_dai_sysclk,
  576. .set_fmt = ux500_msp_dai_set_dai_fmt,
  577. .set_tdm_slot = ux500_msp_dai_set_tdm_slot,
  578. .startup = ux500_msp_dai_startup,
  579. .shutdown = ux500_msp_dai_shutdown,
  580. .prepare = ux500_msp_dai_prepare,
  581. .trigger = ux500_msp_dai_trigger,
  582. .hw_params = ux500_msp_dai_hw_params,
  583. }
  584. };
  585. static struct snd_soc_dai_driver ux500_msp_dai_drv = {
  586. .probe = ux500_msp_dai_probe,
  587. .suspend = NULL,
  588. .resume = NULL,
  589. .playback.channels_min = UX500_MSP_MIN_CHANNELS,
  590. .playback.channels_max = UX500_MSP_MAX_CHANNELS,
  591. .playback.rates = UX500_I2S_RATES,
  592. .playback.formats = UX500_I2S_FORMATS,
  593. .capture.channels_min = UX500_MSP_MIN_CHANNELS,
  594. .capture.channels_max = UX500_MSP_MAX_CHANNELS,
  595. .capture.rates = UX500_I2S_RATES,
  596. .capture.formats = UX500_I2S_FORMATS,
  597. .ops = ux500_msp_dai_ops,
  598. };
  599. static const struct snd_soc_component_driver ux500_msp_component = {
  600. .name = "ux500-msp",
  601. };
  602. static int ux500_msp_drv_probe(struct platform_device *pdev)
  603. {
  604. struct ux500_msp_i2s_drvdata *drvdata;
  605. struct msp_i2s_platform_data *pdata = pdev->dev.platform_data;
  606. struct device_node *np = pdev->dev.of_node;
  607. int ret = 0;
  608. if (!pdata && !np) {
  609. dev_err(&pdev->dev, "No platform data or Device Tree found\n");
  610. return -ENODEV;
  611. }
  612. drvdata = devm_kzalloc(&pdev->dev,
  613. sizeof(struct ux500_msp_i2s_drvdata),
  614. GFP_KERNEL);
  615. if (!drvdata)
  616. return -ENOMEM;
  617. drvdata->fmt = 0;
  618. drvdata->slots = 1;
  619. drvdata->tx_mask = 0x01;
  620. drvdata->rx_mask = 0x01;
  621. drvdata->slot_width = 16;
  622. drvdata->master_clk = MSP_INPUT_FREQ_APB;
  623. drvdata->reg_vape = devm_regulator_get(&pdev->dev, "v-ape");
  624. if (IS_ERR(drvdata->reg_vape)) {
  625. ret = (int)PTR_ERR(drvdata->reg_vape);
  626. dev_err(&pdev->dev,
  627. "%s: ERROR: Failed to get Vape supply (%d)!\n",
  628. __func__, ret);
  629. return ret;
  630. }
  631. prcmu_qos_add_requirement(PRCMU_QOS_APE_OPP, (char *)pdev->name, 50);
  632. drvdata->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  633. if (IS_ERR(drvdata->pclk)) {
  634. ret = (int)PTR_ERR(drvdata->pclk);
  635. dev_err(&pdev->dev,
  636. "%s: ERROR: devm_clk_get of pclk failed (%d)!\n",
  637. __func__, ret);
  638. return ret;
  639. }
  640. drvdata->clk = devm_clk_get(&pdev->dev, NULL);
  641. if (IS_ERR(drvdata->clk)) {
  642. ret = (int)PTR_ERR(drvdata->clk);
  643. dev_err(&pdev->dev,
  644. "%s: ERROR: devm_clk_get failed (%d)!\n",
  645. __func__, ret);
  646. return ret;
  647. }
  648. ret = ux500_msp_i2s_init_msp(pdev, &drvdata->msp,
  649. pdev->dev.platform_data);
  650. if (!drvdata->msp) {
  651. dev_err(&pdev->dev,
  652. "%s: ERROR: Failed to init MSP-struct (%d)!",
  653. __func__, ret);
  654. return ret;
  655. }
  656. dev_set_drvdata(&pdev->dev, drvdata);
  657. ret = snd_soc_register_component(&pdev->dev, &ux500_msp_component,
  658. &ux500_msp_dai_drv, 1);
  659. if (ret < 0) {
  660. dev_err(&pdev->dev, "Error: %s: Failed to register MSP%d!\n",
  661. __func__, drvdata->msp->id);
  662. return ret;
  663. }
  664. ret = ux500_pcm_register_platform(pdev);
  665. if (ret < 0) {
  666. dev_err(&pdev->dev,
  667. "Error: %s: Failed to register PCM platform device!\n",
  668. __func__);
  669. goto err_reg_plat;
  670. }
  671. return 0;
  672. err_reg_plat:
  673. snd_soc_unregister_component(&pdev->dev);
  674. return ret;
  675. }
  676. static int ux500_msp_drv_remove(struct platform_device *pdev)
  677. {
  678. struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
  679. ux500_pcm_unregister_platform(pdev);
  680. snd_soc_unregister_component(&pdev->dev);
  681. prcmu_qos_remove_requirement(PRCMU_QOS_APE_OPP, "ux500_msp_i2s");
  682. ux500_msp_i2s_cleanup_msp(pdev, drvdata->msp);
  683. return 0;
  684. }
  685. static const struct of_device_id ux500_msp_i2s_match[] = {
  686. { .compatible = "stericsson,ux500-msp-i2s", },
  687. {},
  688. };
  689. MODULE_DEVICE_TABLE(of, ux500_msp_i2s_match);
  690. static struct platform_driver msp_i2s_driver = {
  691. .driver = {
  692. .name = "ux500-msp-i2s",
  693. .of_match_table = ux500_msp_i2s_match,
  694. },
  695. .probe = ux500_msp_drv_probe,
  696. .remove = ux500_msp_drv_remove,
  697. };
  698. module_platform_driver(msp_i2s_driver);
  699. MODULE_LICENSE("GPL v2");