uniperif_player.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2015
  3. * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
  4. * for STMicroelectronics.
  5. * License terms: GNU General Public License (GPL), version 2
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <sound/asoundef.h>
  12. #include <sound/soc.h>
  13. #include "uniperif.h"
  14. /*
  15. * Some hardware-related definitions
  16. */
  17. /* sys config registers definitions */
  18. #define SYS_CFG_AUDIO_GLUE 0xA4
  19. #define SYS_CFG_AUDI0_GLUE_PCM_CLKX 8
  20. /*
  21. * Driver specific types.
  22. */
  23. #define UNIPERIF_PLAYER_TYPE_IS_HDMI(p) \
  24. ((p)->info->player_type == SND_ST_UNIPERIF_PLAYER_TYPE_HDMI)
  25. #define UNIPERIF_PLAYER_TYPE_IS_PCM(p) \
  26. ((p)->info->player_type == SND_ST_UNIPERIF_PLAYER_TYPE_PCM)
  27. #define UNIPERIF_PLAYER_TYPE_IS_SPDIF(p) \
  28. ((p)->info->player_type == SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF)
  29. #define UNIPERIF_PLAYER_TYPE_IS_IEC958(p) \
  30. (UNIPERIF_PLAYER_TYPE_IS_HDMI(p) || \
  31. UNIPERIF_PLAYER_TYPE_IS_SPDIF(p))
  32. #define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999
  33. #define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000
  34. /*
  35. * Note: snd_pcm_hardware is linked to DMA controller but is declared here to
  36. * integrate DAI_CPU capability in term of rate and supported channels
  37. */
  38. static const struct snd_pcm_hardware uni_player_pcm_hw = {
  39. .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  40. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP |
  41. SNDRV_PCM_INFO_MMAP_VALID,
  42. .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE,
  43. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  44. .rate_min = 8000,
  45. .rate_max = 192000,
  46. .channels_min = 2,
  47. .channels_max = 8,
  48. .periods_min = 2,
  49. .periods_max = 48,
  50. .period_bytes_min = 128,
  51. .period_bytes_max = 64 * PAGE_SIZE,
  52. .buffer_bytes_max = 256 * PAGE_SIZE
  53. };
  54. static inline int reset_player(struct uniperif *player)
  55. {
  56. int count = 10;
  57. if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
  58. while (GET_UNIPERIF_SOFT_RST_SOFT_RST(player) && count) {
  59. udelay(5);
  60. count--;
  61. }
  62. }
  63. if (!count) {
  64. dev_err(player->dev, "Failed to reset uniperif");
  65. return -EIO;
  66. }
  67. return 0;
  68. }
  69. /*
  70. * uni_player_irq_handler
  71. * In case of error audio stream is stopped; stop action is protected via PCM
  72. * stream lock to avoid race condition with trigger callback.
  73. */
  74. static irqreturn_t uni_player_irq_handler(int irq, void *dev_id)
  75. {
  76. irqreturn_t ret = IRQ_NONE;
  77. struct uniperif *player = dev_id;
  78. unsigned int status;
  79. unsigned int tmp;
  80. if (player->state == UNIPERIF_STATE_STOPPED) {
  81. /* Unexpected IRQ: do nothing */
  82. return IRQ_NONE;
  83. }
  84. /* Get interrupt status & clear them immediately */
  85. status = GET_UNIPERIF_ITS(player);
  86. SET_UNIPERIF_ITS_BCLR(player, status);
  87. /* Check for fifo error (underrun) */
  88. if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(player))) {
  89. dev_err(player->dev, "FIFO underflow error detected");
  90. /* Interrupt is just for information when underflow recovery */
  91. if (player->info->underflow_enabled) {
  92. /* Update state to underflow */
  93. player->state = UNIPERIF_STATE_UNDERFLOW;
  94. } else {
  95. /* Disable interrupt so doesn't continually fire */
  96. SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player);
  97. /* Stop the player */
  98. snd_pcm_stream_lock(player->substream);
  99. snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
  100. snd_pcm_stream_unlock(player->substream);
  101. }
  102. ret = IRQ_HANDLED;
  103. }
  104. /* Check for dma error (overrun) */
  105. if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) {
  106. dev_err(player->dev, "DMA error detected");
  107. /* Disable interrupt so doesn't continually fire */
  108. SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player);
  109. /* Stop the player */
  110. snd_pcm_stream_lock(player->substream);
  111. snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
  112. snd_pcm_stream_unlock(player->substream);
  113. ret = IRQ_HANDLED;
  114. }
  115. /* Check for underflow recovery done */
  116. if (unlikely(status & UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player))) {
  117. if (!player->info->underflow_enabled) {
  118. dev_err(player->dev, "unexpected Underflow recovering");
  119. return -EPERM;
  120. }
  121. /* Read the underflow recovery duration */
  122. tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player);
  123. /* Clear the underflow recovery duration */
  124. SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player);
  125. /* Update state to started */
  126. player->state = UNIPERIF_STATE_STARTED;
  127. ret = IRQ_HANDLED;
  128. }
  129. /* Check if underflow recovery failed */
  130. if (unlikely(status &
  131. UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player))) {
  132. dev_err(player->dev, "Underflow recovery failed");
  133. /* Stop the player */
  134. snd_pcm_stream_lock(player->substream);
  135. snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
  136. snd_pcm_stream_unlock(player->substream);
  137. ret = IRQ_HANDLED;
  138. }
  139. return ret;
  140. }
  141. static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate)
  142. {
  143. int rate_adjusted, rate_achieved, delta, ret;
  144. int adjustment = player->clk_adj;
  145. /*
  146. * a
  147. * F = f + --------- * f = f + d
  148. * 1000000
  149. *
  150. * a
  151. * d = --------- * f
  152. * 1000000
  153. *
  154. * where:
  155. * f - nominal rate
  156. * a - adjustment in ppm (parts per milion)
  157. * F - rate to be set in synthesizer
  158. * d - delta (difference) between f and F
  159. */
  160. if (adjustment < 0) {
  161. /* div64_64 operates on unsigned values... */
  162. delta = -1;
  163. adjustment = -adjustment;
  164. } else {
  165. delta = 1;
  166. }
  167. /* 500000 ppm is 0.5, which is used to round up values */
  168. delta *= (int)div64_u64((uint64_t)rate *
  169. (uint64_t)adjustment + 500000, 1000000);
  170. rate_adjusted = rate + delta;
  171. /* Adjusted rate should never be == 0 */
  172. if (!rate_adjusted)
  173. return -EINVAL;
  174. ret = clk_set_rate(player->clk, rate_adjusted);
  175. if (ret < 0)
  176. return ret;
  177. rate_achieved = clk_get_rate(player->clk);
  178. if (!rate_achieved)
  179. /* If value is 0 means that clock or parent not valid */
  180. return -EINVAL;
  181. /*
  182. * Using ALSA's adjustment control, we can modify the rate to be up
  183. * to twice as much as requested, but no more
  184. */
  185. delta = rate_achieved - rate;
  186. if (delta < 0) {
  187. /* div64_64 operates on unsigned values... */
  188. delta = -delta;
  189. adjustment = -1;
  190. } else {
  191. adjustment = 1;
  192. }
  193. /* Frequency/2 is added to round up result */
  194. adjustment *= (int)div64_u64((uint64_t)delta * 1000000 + rate / 2,
  195. rate);
  196. player->clk_adj = adjustment;
  197. return 0;
  198. }
  199. static void uni_player_set_channel_status(struct uniperif *player,
  200. struct snd_pcm_runtime *runtime)
  201. {
  202. int n;
  203. unsigned int status;
  204. /*
  205. * Some AVRs and TVs require the channel status to contain a correct
  206. * sampling frequency. If no sample rate is already specified, then
  207. * set one.
  208. */
  209. mutex_lock(&player->ctrl_lock);
  210. if (runtime && (player->stream_settings.iec958.status[3]
  211. == IEC958_AES3_CON_FS_NOTID)) {
  212. switch (runtime->rate) {
  213. case 22050:
  214. player->stream_settings.iec958.status[3] =
  215. IEC958_AES3_CON_FS_22050;
  216. break;
  217. case 44100:
  218. player->stream_settings.iec958.status[3] =
  219. IEC958_AES3_CON_FS_44100;
  220. break;
  221. case 88200:
  222. player->stream_settings.iec958.status[3] =
  223. IEC958_AES3_CON_FS_88200;
  224. break;
  225. case 176400:
  226. player->stream_settings.iec958.status[3] =
  227. IEC958_AES3_CON_FS_176400;
  228. break;
  229. case 24000:
  230. player->stream_settings.iec958.status[3] =
  231. IEC958_AES3_CON_FS_24000;
  232. break;
  233. case 48000:
  234. player->stream_settings.iec958.status[3] =
  235. IEC958_AES3_CON_FS_48000;
  236. break;
  237. case 96000:
  238. player->stream_settings.iec958.status[3] =
  239. IEC958_AES3_CON_FS_96000;
  240. break;
  241. case 192000:
  242. player->stream_settings.iec958.status[3] =
  243. IEC958_AES3_CON_FS_192000;
  244. break;
  245. case 32000:
  246. player->stream_settings.iec958.status[3] =
  247. IEC958_AES3_CON_FS_32000;
  248. break;
  249. default:
  250. /* Mark as sampling frequency not indicated */
  251. player->stream_settings.iec958.status[3] =
  252. IEC958_AES3_CON_FS_NOTID;
  253. break;
  254. }
  255. }
  256. /* Audio mode:
  257. * Use audio mode status to select PCM or encoded mode
  258. */
  259. if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO)
  260. player->stream_settings.encoding_mode =
  261. UNIPERIF_IEC958_ENCODING_MODE_ENCODED;
  262. else
  263. player->stream_settings.encoding_mode =
  264. UNIPERIF_IEC958_ENCODING_MODE_PCM;
  265. if (player->stream_settings.encoding_mode ==
  266. UNIPERIF_IEC958_ENCODING_MODE_PCM)
  267. /* Clear user validity bits */
  268. SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
  269. else
  270. /* Set user validity bits */
  271. SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1);
  272. /* Program the new channel status */
  273. for (n = 0; n < 6; ++n) {
  274. status =
  275. player->stream_settings.iec958.status[0 + (n * 4)] & 0xf;
  276. status |=
  277. player->stream_settings.iec958.status[1 + (n * 4)] << 8;
  278. status |=
  279. player->stream_settings.iec958.status[2 + (n * 4)] << 16;
  280. status |=
  281. player->stream_settings.iec958.status[3 + (n * 4)] << 24;
  282. SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status);
  283. }
  284. mutex_unlock(&player->ctrl_lock);
  285. /* Update the channel status */
  286. if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
  287. SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
  288. else
  289. SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
  290. }
  291. static int uni_player_prepare_iec958(struct uniperif *player,
  292. struct snd_pcm_runtime *runtime)
  293. {
  294. int clk_div;
  295. clk_div = player->mclk / runtime->rate;
  296. /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */
  297. if ((clk_div % 128) || (clk_div <= 0)) {
  298. dev_err(player->dev, "%s: invalid clk_div %d",
  299. __func__, clk_div);
  300. return -EINVAL;
  301. }
  302. switch (runtime->format) {
  303. case SNDRV_PCM_FORMAT_S16_LE:
  304. /* 16/16 memory format */
  305. SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
  306. /* 16-bits per sub-frame */
  307. SET_UNIPERIF_I2S_FMT_NBIT_32(player);
  308. /* Set 16-bit sample precision */
  309. SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
  310. break;
  311. case SNDRV_PCM_FORMAT_S32_LE:
  312. /* 16/0 memory format */
  313. SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
  314. /* 32-bits per sub-frame */
  315. SET_UNIPERIF_I2S_FMT_NBIT_32(player);
  316. /* Set 24-bit sample precision */
  317. SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player);
  318. break;
  319. default:
  320. dev_err(player->dev, "format not supported");
  321. return -EINVAL;
  322. }
  323. /* Set parity to be calculated by the hardware */
  324. SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player);
  325. /* Set channel status bits to be inserted by the hardware */
  326. SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player);
  327. /* Set user data bits to be inserted by the hardware */
  328. SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player);
  329. /* Set validity bits to be inserted by the hardware */
  330. SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player);
  331. /* Set full software control to disabled */
  332. SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player);
  333. SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player);
  334. /* Update the channel status */
  335. uni_player_set_channel_status(player, runtime);
  336. /* Clear the user validity user bits */
  337. SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
  338. /* Disable one-bit audio mode */
  339. SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
  340. /* Enable consecutive frames repetition of Z preamble (not for HBRA) */
  341. SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player);
  342. /* Change to SUF0_SUBF1 and left/right channels swap! */
  343. SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player);
  344. /* Set data output as MSB first */
  345. SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
  346. if (player->stream_settings.encoding_mode ==
  347. UNIPERIF_IEC958_ENCODING_MODE_ENCODED)
  348. SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player);
  349. else
  350. SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player);
  351. SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
  352. /* Set rounding to off */
  353. SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
  354. /* Set clock divisor */
  355. SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128);
  356. /* Set the spdif latency to not wait before starting player */
  357. SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
  358. /*
  359. * Ensure iec958 formatting is off. It will be enabled in function
  360. * uni_player_start() at the same time as the operation
  361. * mode is set to work around a silicon issue.
  362. */
  363. if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
  364. SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
  365. else
  366. SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
  367. return 0;
  368. }
  369. static int uni_player_prepare_pcm(struct uniperif *player,
  370. struct snd_pcm_runtime *runtime)
  371. {
  372. int output_frame_size, slot_width, clk_div;
  373. /* Force slot width to 32 in I2S mode (HW constraint) */
  374. if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
  375. SND_SOC_DAIFMT_I2S) {
  376. slot_width = 32;
  377. } else {
  378. switch (runtime->format) {
  379. case SNDRV_PCM_FORMAT_S16_LE:
  380. slot_width = 16;
  381. break;
  382. default:
  383. slot_width = 32;
  384. break;
  385. }
  386. }
  387. output_frame_size = slot_width * runtime->channels;
  388. clk_div = player->mclk / runtime->rate;
  389. /*
  390. * For 32 bits subframe clk_div must be a multiple of 128,
  391. * for 16 bits must be a multiple of 64
  392. */
  393. if ((slot_width == 32) && (clk_div % 128)) {
  394. dev_err(player->dev, "%s: invalid clk_div", __func__);
  395. return -EINVAL;
  396. }
  397. if ((slot_width == 16) && (clk_div % 64)) {
  398. dev_err(player->dev, "%s: invalid clk_div", __func__);
  399. return -EINVAL;
  400. }
  401. /*
  402. * Number of bits per subframe (which is one channel sample)
  403. * on output - Transfer 16 or 32 bits from FIFO
  404. */
  405. switch (slot_width) {
  406. case 32:
  407. SET_UNIPERIF_I2S_FMT_NBIT_32(player);
  408. SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
  409. break;
  410. case 16:
  411. SET_UNIPERIF_I2S_FMT_NBIT_16(player);
  412. SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
  413. break;
  414. default:
  415. dev_err(player->dev, "subframe format not supported");
  416. return -EINVAL;
  417. }
  418. /* Configure data memory format */
  419. switch (runtime->format) {
  420. case SNDRV_PCM_FORMAT_S16_LE:
  421. /* One data word contains two samples */
  422. SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
  423. break;
  424. case SNDRV_PCM_FORMAT_S32_LE:
  425. /*
  426. * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
  427. * on the left than zeros (if less than 32 bytes)"... ;-)
  428. */
  429. SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
  430. break;
  431. default:
  432. dev_err(player->dev, "format not supported");
  433. return -EINVAL;
  434. }
  435. /* Set rounding to off */
  436. SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
  437. /* Set clock divisor */
  438. SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size));
  439. /* Number of channelsmust be even*/
  440. if ((runtime->channels % 2) || (runtime->channels < 2) ||
  441. (runtime->channels > 10)) {
  442. dev_err(player->dev, "%s: invalid nb of channels", __func__);
  443. return -EINVAL;
  444. }
  445. SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
  446. /* Set 1-bit audio format to disabled */
  447. SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
  448. SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
  449. SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
  450. /* No iec958 formatting as outputting to DAC */
  451. SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
  452. return 0;
  453. }
  454. /*
  455. * ALSA uniperipheral iec958 controls
  456. */
  457. static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol,
  458. struct snd_ctl_elem_info *uinfo)
  459. {
  460. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  461. uinfo->count = 1;
  462. return 0;
  463. }
  464. static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol,
  465. struct snd_ctl_elem_value *ucontrol)
  466. {
  467. struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
  468. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  469. struct uniperif *player = priv->dai_data.uni;
  470. struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
  471. mutex_lock(&player->ctrl_lock);
  472. ucontrol->value.iec958.status[0] = iec958->status[0];
  473. ucontrol->value.iec958.status[1] = iec958->status[1];
  474. ucontrol->value.iec958.status[2] = iec958->status[2];
  475. ucontrol->value.iec958.status[3] = iec958->status[3];
  476. mutex_unlock(&player->ctrl_lock);
  477. return 0;
  478. }
  479. static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol,
  480. struct snd_ctl_elem_value *ucontrol)
  481. {
  482. struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
  483. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  484. struct uniperif *player = priv->dai_data.uni;
  485. struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
  486. mutex_lock(&player->ctrl_lock);
  487. iec958->status[0] = ucontrol->value.iec958.status[0];
  488. iec958->status[1] = ucontrol->value.iec958.status[1];
  489. iec958->status[2] = ucontrol->value.iec958.status[2];
  490. iec958->status[3] = ucontrol->value.iec958.status[3];
  491. mutex_unlock(&player->ctrl_lock);
  492. uni_player_set_channel_status(player, NULL);
  493. return 0;
  494. }
  495. static struct snd_kcontrol_new uni_player_iec958_ctl = {
  496. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  497. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  498. .info = uni_player_ctl_iec958_info,
  499. .get = uni_player_ctl_iec958_get,
  500. .put = uni_player_ctl_iec958_put,
  501. };
  502. /*
  503. * uniperif rate adjustement control
  504. */
  505. static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol,
  506. struct snd_ctl_elem_info *uinfo)
  507. {
  508. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  509. uinfo->count = 1;
  510. uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN;
  511. uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX;
  512. uinfo->value.integer.step = 1;
  513. return 0;
  514. }
  515. static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol,
  516. struct snd_ctl_elem_value *ucontrol)
  517. {
  518. struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
  519. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  520. struct uniperif *player = priv->dai_data.uni;
  521. mutex_lock(&player->ctrl_lock);
  522. ucontrol->value.integer.value[0] = player->clk_adj;
  523. mutex_unlock(&player->ctrl_lock);
  524. return 0;
  525. }
  526. static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol,
  527. struct snd_ctl_elem_value *ucontrol)
  528. {
  529. struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
  530. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  531. struct uniperif *player = priv->dai_data.uni;
  532. int ret = 0;
  533. if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) ||
  534. (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX))
  535. return -EINVAL;
  536. mutex_lock(&player->ctrl_lock);
  537. player->clk_adj = ucontrol->value.integer.value[0];
  538. if (player->mclk)
  539. ret = uni_player_clk_set_rate(player, player->mclk);
  540. mutex_unlock(&player->ctrl_lock);
  541. return ret;
  542. }
  543. static struct snd_kcontrol_new uni_player_clk_adj_ctl = {
  544. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  545. .name = "PCM Playback Oversampling Freq. Adjustment",
  546. .info = snd_sti_clk_adjustment_info,
  547. .get = snd_sti_clk_adjustment_get,
  548. .put = snd_sti_clk_adjustment_put,
  549. };
  550. static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = {
  551. &uni_player_clk_adj_ctl,
  552. };
  553. static struct snd_kcontrol_new *snd_sti_iec_ctl[] = {
  554. &uni_player_iec958_ctl,
  555. &uni_player_clk_adj_ctl,
  556. };
  557. static int uni_player_startup(struct snd_pcm_substream *substream,
  558. struct snd_soc_dai *dai)
  559. {
  560. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  561. struct uniperif *player = priv->dai_data.uni;
  562. player->substream = substream;
  563. player->clk_adj = 0;
  564. return 0;
  565. }
  566. static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  567. unsigned int freq, int dir)
  568. {
  569. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  570. struct uniperif *player = priv->dai_data.uni;
  571. int ret;
  572. if (dir == SND_SOC_CLOCK_IN)
  573. return 0;
  574. if (clk_id != 0)
  575. return -EINVAL;
  576. mutex_lock(&player->ctrl_lock);
  577. ret = uni_player_clk_set_rate(player, freq);
  578. if (!ret)
  579. player->mclk = freq;
  580. mutex_unlock(&player->ctrl_lock);
  581. return ret;
  582. }
  583. static int uni_player_prepare(struct snd_pcm_substream *substream,
  584. struct snd_soc_dai *dai)
  585. {
  586. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  587. struct uniperif *player = priv->dai_data.uni;
  588. struct snd_pcm_runtime *runtime = substream->runtime;
  589. int transfer_size, trigger_limit;
  590. int ret;
  591. /* The player should be stopped */
  592. if (player->state != UNIPERIF_STATE_STOPPED) {
  593. dev_err(player->dev, "%s: invalid player state %d", __func__,
  594. player->state);
  595. return -EINVAL;
  596. }
  597. /* Calculate transfer size (in fifo cells and bytes) for frame count */
  598. transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
  599. /* Calculate number of empty cells available before asserting DREQ */
  600. if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
  601. trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
  602. } else {
  603. /*
  604. * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
  605. * FDMA_TRIGGER_LIMIT also controls when the state switches
  606. * from OFF or STANDBY to AUDIO DATA.
  607. */
  608. trigger_limit = transfer_size;
  609. }
  610. /* Trigger limit must be an even number */
  611. if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
  612. (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
  613. dev_err(player->dev, "invalid trigger limit %d", trigger_limit);
  614. return -EINVAL;
  615. }
  616. SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
  617. /* Uniperipheral setup depends on player type */
  618. switch (player->info->player_type) {
  619. case SND_ST_UNIPERIF_PLAYER_TYPE_HDMI:
  620. ret = uni_player_prepare_iec958(player, runtime);
  621. break;
  622. case SND_ST_UNIPERIF_PLAYER_TYPE_PCM:
  623. ret = uni_player_prepare_pcm(player, runtime);
  624. break;
  625. case SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF:
  626. ret = uni_player_prepare_iec958(player, runtime);
  627. break;
  628. default:
  629. dev_err(player->dev, "invalid player type");
  630. return -EINVAL;
  631. }
  632. if (ret)
  633. return ret;
  634. switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
  635. case SND_SOC_DAIFMT_NB_NF:
  636. SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
  637. SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
  638. break;
  639. case SND_SOC_DAIFMT_NB_IF:
  640. SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
  641. SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
  642. break;
  643. case SND_SOC_DAIFMT_IB_NF:
  644. SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
  645. SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
  646. break;
  647. case SND_SOC_DAIFMT_IB_IF:
  648. SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
  649. SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
  650. break;
  651. }
  652. switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  653. case SND_SOC_DAIFMT_I2S:
  654. SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
  655. SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
  656. break;
  657. case SND_SOC_DAIFMT_LEFT_J:
  658. SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
  659. SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
  660. break;
  661. case SND_SOC_DAIFMT_RIGHT_J:
  662. SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
  663. SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
  664. break;
  665. default:
  666. dev_err(player->dev, "format not supported");
  667. return -EINVAL;
  668. }
  669. SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
  670. /* Reset uniperipheral player */
  671. SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
  672. return reset_player(player);
  673. }
  674. static int uni_player_start(struct uniperif *player)
  675. {
  676. int ret;
  677. /* The player should be stopped */
  678. if (player->state != UNIPERIF_STATE_STOPPED) {
  679. dev_err(player->dev, "%s: invalid player state", __func__);
  680. return -EINVAL;
  681. }
  682. ret = clk_prepare_enable(player->clk);
  683. if (ret) {
  684. dev_err(player->dev, "%s: Failed to enable clock", __func__);
  685. return ret;
  686. }
  687. /* Clear any pending interrupts */
  688. SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
  689. /* Set the interrupt mask */
  690. SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
  691. SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
  692. /* Enable underflow recovery interrupts */
  693. if (player->info->underflow_enabled) {
  694. SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
  695. SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
  696. }
  697. /* Reset uniperipheral player */
  698. SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
  699. ret = reset_player(player);
  700. if (ret < 0)
  701. return ret;
  702. /*
  703. * Does not use IEC61937 features of the uniperipheral hardware.
  704. * Instead it performs IEC61937 in software and inserts it directly
  705. * into the audio data stream. As such, when encoded mode is selected,
  706. * linear pcm mode is still used, but with the differences of the
  707. * channel status bits set for encoded mode and the validity bits set.
  708. */
  709. SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
  710. /*
  711. * If iec958 formatting is required for hdmi or spdif, then it must be
  712. * enabled after the operation mode is set. If set prior to this, it
  713. * will not take affect and hang the player.
  714. */
  715. if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
  716. if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player))
  717. SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
  718. /* Force channel status update (no update if clk disable) */
  719. if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
  720. SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
  721. else
  722. SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
  723. /* Update state to started */
  724. player->state = UNIPERIF_STATE_STARTED;
  725. return 0;
  726. }
  727. static int uni_player_stop(struct uniperif *player)
  728. {
  729. int ret;
  730. /* The player should not be in stopped state */
  731. if (player->state == UNIPERIF_STATE_STOPPED) {
  732. dev_err(player->dev, "%s: invalid player state", __func__);
  733. return -EINVAL;
  734. }
  735. /* Turn the player off */
  736. SET_UNIPERIF_CTRL_OPERATION_OFF(player);
  737. /* Soft reset the player */
  738. SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
  739. ret = reset_player(player);
  740. if (ret < 0)
  741. return ret;
  742. /* Disable interrupts */
  743. SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
  744. /* Disable clock */
  745. clk_disable_unprepare(player->clk);
  746. /* Update state to stopped and return */
  747. player->state = UNIPERIF_STATE_STOPPED;
  748. return 0;
  749. }
  750. int uni_player_resume(struct uniperif *player)
  751. {
  752. int ret;
  753. /* Select the frequency synthesizer clock */
  754. if (player->clk_sel) {
  755. ret = regmap_field_write(player->clk_sel, 1);
  756. if (ret) {
  757. dev_err(player->dev,
  758. "%s: Failed to select freq synth clock",
  759. __func__);
  760. return ret;
  761. }
  762. }
  763. SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
  764. SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
  765. SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
  766. SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
  767. return 0;
  768. }
  769. EXPORT_SYMBOL_GPL(uni_player_resume);
  770. static int uni_player_trigger(struct snd_pcm_substream *substream,
  771. int cmd, struct snd_soc_dai *dai)
  772. {
  773. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  774. struct uniperif *player = priv->dai_data.uni;
  775. switch (cmd) {
  776. case SNDRV_PCM_TRIGGER_START:
  777. return uni_player_start(player);
  778. case SNDRV_PCM_TRIGGER_STOP:
  779. return uni_player_stop(player);
  780. case SNDRV_PCM_TRIGGER_RESUME:
  781. return uni_player_resume(player);
  782. default:
  783. return -EINVAL;
  784. }
  785. }
  786. static void uni_player_shutdown(struct snd_pcm_substream *substream,
  787. struct snd_soc_dai *dai)
  788. {
  789. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  790. struct uniperif *player = priv->dai_data.uni;
  791. if (player->state != UNIPERIF_STATE_STOPPED)
  792. /* Stop the player */
  793. uni_player_stop(player);
  794. player->substream = NULL;
  795. }
  796. static int uni_player_parse_dt_clk_glue(struct platform_device *pdev,
  797. struct uniperif *player)
  798. {
  799. int bit_offset;
  800. struct device_node *node = pdev->dev.of_node;
  801. struct regmap *regmap;
  802. bit_offset = SYS_CFG_AUDI0_GLUE_PCM_CLKX + player->info->id;
  803. regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
  804. if (regmap) {
  805. struct reg_field regfield =
  806. REG_FIELD(SYS_CFG_AUDIO_GLUE, bit_offset, bit_offset);
  807. player->clk_sel = regmap_field_alloc(regmap, regfield);
  808. } else {
  809. dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
  810. return -EINVAL;
  811. }
  812. return 0;
  813. }
  814. static int uni_player_parse_dt(struct platform_device *pdev,
  815. struct uniperif *player)
  816. {
  817. struct uniperif_info *info;
  818. struct device *dev = &pdev->dev;
  819. struct device_node *pnode = pdev->dev.of_node;
  820. const char *mode;
  821. /* Allocate memory for the info structure */
  822. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  823. if (!info)
  824. return -ENOMEM;
  825. if (of_property_read_u32(pnode, "st,version", &player->ver) ||
  826. player->ver == SND_ST_UNIPERIF_VERSION_UNKNOWN) {
  827. dev_err(dev, "Unknown uniperipheral version ");
  828. return -EINVAL;
  829. }
  830. /* Underflow recovery is only supported on later ip revisions */
  831. if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
  832. info->underflow_enabled = 1;
  833. if (of_property_read_u32(pnode, "st,uniperiph-id", &info->id)) {
  834. dev_err(dev, "uniperipheral id not defined");
  835. return -EINVAL;
  836. }
  837. /* Read the device mode property */
  838. if (of_property_read_string(pnode, "st,mode", &mode)) {
  839. dev_err(dev, "uniperipheral mode not defined");
  840. return -EINVAL;
  841. }
  842. if (strcasecmp(mode, "hdmi") == 0)
  843. info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_HDMI;
  844. else if (strcasecmp(mode, "pcm") == 0)
  845. info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_PCM;
  846. else if (strcasecmp(mode, "spdif") == 0)
  847. info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF;
  848. else
  849. info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_NONE;
  850. /* Save the info structure */
  851. player->info = info;
  852. /* Get the PCM_CLK_SEL bit from audio-glue-ctrl SoC register */
  853. if (uni_player_parse_dt_clk_glue(pdev, player))
  854. return -EINVAL;
  855. return 0;
  856. }
  857. static const struct snd_soc_dai_ops uni_player_dai_ops = {
  858. .startup = uni_player_startup,
  859. .shutdown = uni_player_shutdown,
  860. .prepare = uni_player_prepare,
  861. .trigger = uni_player_trigger,
  862. .hw_params = sti_uniperiph_dai_hw_params,
  863. .set_fmt = sti_uniperiph_dai_set_fmt,
  864. .set_sysclk = uni_player_set_sysclk
  865. };
  866. int uni_player_init(struct platform_device *pdev,
  867. struct uniperif *player)
  868. {
  869. int ret = 0;
  870. player->dev = &pdev->dev;
  871. player->state = UNIPERIF_STATE_STOPPED;
  872. player->hw = &uni_player_pcm_hw;
  873. player->dai_ops = &uni_player_dai_ops;
  874. ret = uni_player_parse_dt(pdev, player);
  875. if (ret < 0) {
  876. dev_err(player->dev, "Failed to parse DeviceTree");
  877. return ret;
  878. }
  879. /* Get uniperif resource */
  880. player->clk = of_clk_get(pdev->dev.of_node, 0);
  881. if (IS_ERR(player->clk))
  882. ret = PTR_ERR(player->clk);
  883. /* Select the frequency synthesizer clock */
  884. if (player->clk_sel) {
  885. ret = regmap_field_write(player->clk_sel, 1);
  886. if (ret) {
  887. dev_err(player->dev,
  888. "%s: Failed to select freq synth clock",
  889. __func__);
  890. return ret;
  891. }
  892. }
  893. ret = devm_request_irq(&pdev->dev, player->irq,
  894. uni_player_irq_handler, IRQF_SHARED,
  895. dev_name(&pdev->dev), player);
  896. if (ret < 0)
  897. return ret;
  898. mutex_init(&player->ctrl_lock);
  899. /* Ensure that disabled by default */
  900. SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
  901. SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
  902. SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
  903. SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
  904. if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player)) {
  905. /* Set default iec958 status bits */
  906. /* Consumer, PCM, copyright, 2ch, mode 0 */
  907. player->stream_settings.iec958.status[0] = 0x00;
  908. /* Broadcast reception category */
  909. player->stream_settings.iec958.status[1] =
  910. IEC958_AES1_CON_GENERAL;
  911. /* Do not take into account source or channel number */
  912. player->stream_settings.iec958.status[2] =
  913. IEC958_AES2_CON_SOURCE_UNSPEC;
  914. /* Sampling frequency not indicated */
  915. player->stream_settings.iec958.status[3] =
  916. IEC958_AES3_CON_FS_NOTID;
  917. /* Max sample word 24-bit, sample word length not indicated */
  918. player->stream_settings.iec958.status[4] =
  919. IEC958_AES4_CON_MAX_WORDLEN_24 |
  920. IEC958_AES4_CON_WORDLEN_24_20;
  921. player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl);
  922. player->snd_ctrls = snd_sti_iec_ctl[0];
  923. } else {
  924. player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl);
  925. player->snd_ctrls = snd_sti_pcm_ctl[0];
  926. }
  927. return 0;
  928. }
  929. EXPORT_SYMBOL_GPL(uni_player_init);