sti_uniperif.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2015
  4. * Authors: Arnaud Pouliquen <[email protected]>
  5. * for STMicroelectronics.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/pinctrl/consumer.h>
  9. #include <linux/delay.h>
  10. #include "uniperif.h"
  11. /*
  12. * User frame size shall be 2, 4, 6 or 8 32-bits words length
  13. * (i.e. 8, 16, 24 or 32 bytes)
  14. * This constraint comes from allowed values for
  15. * UNIPERIF_I2S_FMT_NUM_CH register
  16. */
  17. #define UNIPERIF_MAX_FRAME_SZ 0x20
  18. #define UNIPERIF_ALLOWED_FRAME_SZ (0x08 | 0x10 | 0x18 | UNIPERIF_MAX_FRAME_SZ)
  19. struct sti_uniperiph_dev_data {
  20. unsigned int id; /* Nb available player instances */
  21. unsigned int version; /* player IP version */
  22. unsigned int stream;
  23. const char *dai_names;
  24. enum uniperif_type type;
  25. };
  26. static const struct sti_uniperiph_dev_data sti_uniplayer_hdmi = {
  27. .id = 0,
  28. .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0,
  29. .stream = SNDRV_PCM_STREAM_PLAYBACK,
  30. .dai_names = "Uni Player #0 (HDMI)",
  31. .type = SND_ST_UNIPERIF_TYPE_HDMI
  32. };
  33. static const struct sti_uniperiph_dev_data sti_uniplayer_pcm_out = {
  34. .id = 1,
  35. .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0,
  36. .stream = SNDRV_PCM_STREAM_PLAYBACK,
  37. .dai_names = "Uni Player #1 (PCM OUT)",
  38. .type = SND_ST_UNIPERIF_TYPE_PCM | SND_ST_UNIPERIF_TYPE_TDM,
  39. };
  40. static const struct sti_uniperiph_dev_data sti_uniplayer_dac = {
  41. .id = 2,
  42. .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0,
  43. .stream = SNDRV_PCM_STREAM_PLAYBACK,
  44. .dai_names = "Uni Player #2 (DAC)",
  45. .type = SND_ST_UNIPERIF_TYPE_PCM,
  46. };
  47. static const struct sti_uniperiph_dev_data sti_uniplayer_spdif = {
  48. .id = 3,
  49. .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0,
  50. .stream = SNDRV_PCM_STREAM_PLAYBACK,
  51. .dai_names = "Uni Player #3 (SPDIF)",
  52. .type = SND_ST_UNIPERIF_TYPE_SPDIF
  53. };
  54. static const struct sti_uniperiph_dev_data sti_unireader_pcm_in = {
  55. .id = 0,
  56. .version = SND_ST_UNIPERIF_VERSION_UNI_RDR_1_0,
  57. .stream = SNDRV_PCM_STREAM_CAPTURE,
  58. .dai_names = "Uni Reader #0 (PCM IN)",
  59. .type = SND_ST_UNIPERIF_TYPE_PCM | SND_ST_UNIPERIF_TYPE_TDM,
  60. };
  61. static const struct sti_uniperiph_dev_data sti_unireader_hdmi_in = {
  62. .id = 1,
  63. .version = SND_ST_UNIPERIF_VERSION_UNI_RDR_1_0,
  64. .stream = SNDRV_PCM_STREAM_CAPTURE,
  65. .dai_names = "Uni Reader #1 (HDMI IN)",
  66. .type = SND_ST_UNIPERIF_TYPE_PCM,
  67. };
  68. static const struct of_device_id snd_soc_sti_match[] = {
  69. { .compatible = "st,stih407-uni-player-hdmi",
  70. .data = &sti_uniplayer_hdmi
  71. },
  72. { .compatible = "st,stih407-uni-player-pcm-out",
  73. .data = &sti_uniplayer_pcm_out
  74. },
  75. { .compatible = "st,stih407-uni-player-dac",
  76. .data = &sti_uniplayer_dac
  77. },
  78. { .compatible = "st,stih407-uni-player-spdif",
  79. .data = &sti_uniplayer_spdif
  80. },
  81. { .compatible = "st,stih407-uni-reader-pcm_in",
  82. .data = &sti_unireader_pcm_in
  83. },
  84. { .compatible = "st,stih407-uni-reader-hdmi",
  85. .data = &sti_unireader_hdmi_in
  86. },
  87. {},
  88. };
  89. MODULE_DEVICE_TABLE(of, snd_soc_sti_match);
  90. int sti_uniperiph_reset(struct uniperif *uni)
  91. {
  92. int count = 10;
  93. /* Reset uniperipheral uni */
  94. SET_UNIPERIF_SOFT_RST_SOFT_RST(uni);
  95. if (uni->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
  96. while (GET_UNIPERIF_SOFT_RST_SOFT_RST(uni) && count) {
  97. udelay(5);
  98. count--;
  99. }
  100. }
  101. if (!count) {
  102. dev_err(uni->dev, "Failed to reset uniperif\n");
  103. return -EIO;
  104. }
  105. return 0;
  106. }
  107. int sti_uniperiph_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  108. unsigned int rx_mask, int slots,
  109. int slot_width)
  110. {
  111. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  112. struct uniperif *uni = priv->dai_data.uni;
  113. int i, frame_size, avail_slots;
  114. if (!UNIPERIF_TYPE_IS_TDM(uni)) {
  115. dev_err(uni->dev, "cpu dai not in tdm mode\n");
  116. return -EINVAL;
  117. }
  118. /* store info in unip context */
  119. uni->tdm_slot.slots = slots;
  120. uni->tdm_slot.slot_width = slot_width;
  121. /* unip is unidirectionnal */
  122. uni->tdm_slot.mask = (tx_mask != 0) ? tx_mask : rx_mask;
  123. /* number of available timeslots */
  124. for (i = 0, avail_slots = 0; i < uni->tdm_slot.slots; i++) {
  125. if ((uni->tdm_slot.mask >> i) & 0x01)
  126. avail_slots++;
  127. }
  128. uni->tdm_slot.avail_slots = avail_slots;
  129. /* frame size in bytes */
  130. frame_size = uni->tdm_slot.avail_slots * uni->tdm_slot.slot_width / 8;
  131. /* check frame size is allowed */
  132. if ((frame_size > UNIPERIF_MAX_FRAME_SZ) ||
  133. (frame_size & ~(int)UNIPERIF_ALLOWED_FRAME_SZ)) {
  134. dev_err(uni->dev, "frame size not allowed: %d bytes\n",
  135. frame_size);
  136. return -EINVAL;
  137. }
  138. return 0;
  139. }
  140. int sti_uniperiph_fix_tdm_chan(struct snd_pcm_hw_params *params,
  141. struct snd_pcm_hw_rule *rule)
  142. {
  143. struct uniperif *uni = rule->private;
  144. struct snd_interval t;
  145. t.min = uni->tdm_slot.avail_slots;
  146. t.max = uni->tdm_slot.avail_slots;
  147. t.openmin = 0;
  148. t.openmax = 0;
  149. t.integer = 0;
  150. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  151. }
  152. int sti_uniperiph_fix_tdm_format(struct snd_pcm_hw_params *params,
  153. struct snd_pcm_hw_rule *rule)
  154. {
  155. struct uniperif *uni = rule->private;
  156. struct snd_mask *maskp = hw_param_mask(params, rule->var);
  157. u64 format;
  158. switch (uni->tdm_slot.slot_width) {
  159. case 16:
  160. format = SNDRV_PCM_FMTBIT_S16_LE;
  161. break;
  162. case 32:
  163. format = SNDRV_PCM_FMTBIT_S32_LE;
  164. break;
  165. default:
  166. dev_err(uni->dev, "format not supported: %d bits\n",
  167. uni->tdm_slot.slot_width);
  168. return -EINVAL;
  169. }
  170. maskp->bits[0] &= (u_int32_t)format;
  171. maskp->bits[1] &= (u_int32_t)(format >> 32);
  172. /* clear remaining indexes */
  173. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX - 64) / 8);
  174. if (!maskp->bits[0] && !maskp->bits[1])
  175. return -EINVAL;
  176. return 0;
  177. }
  178. int sti_uniperiph_get_tdm_word_pos(struct uniperif *uni,
  179. unsigned int *word_pos)
  180. {
  181. int slot_width = uni->tdm_slot.slot_width / 8;
  182. int slots_num = uni->tdm_slot.slots;
  183. unsigned int slots_mask = uni->tdm_slot.mask;
  184. int i, j, k;
  185. unsigned int word16_pos[4];
  186. /* word16_pos:
  187. * word16_pos[0] = WORDX_LSB
  188. * word16_pos[1] = WORDX_MSB,
  189. * word16_pos[2] = WORDX+1_LSB
  190. * word16_pos[3] = WORDX+1_MSB
  191. */
  192. /* set unip word position */
  193. for (i = 0, j = 0, k = 0; (i < slots_num) && (k < WORD_MAX); i++) {
  194. if ((slots_mask >> i) & 0x01) {
  195. word16_pos[j] = i * slot_width;
  196. if (slot_width == 4) {
  197. word16_pos[j + 1] = word16_pos[j] + 2;
  198. j++;
  199. }
  200. j++;
  201. if (j > 3) {
  202. word_pos[k] = word16_pos[1] |
  203. (word16_pos[0] << 8) |
  204. (word16_pos[3] << 16) |
  205. (word16_pos[2] << 24);
  206. j = 0;
  207. k++;
  208. }
  209. }
  210. }
  211. return 0;
  212. }
  213. /*
  214. * sti_uniperiph_dai_create_ctrl
  215. * This function is used to create Ctrl associated to DAI but also pcm device.
  216. * Request is done by front end to associate ctrl with pcm device id
  217. */
  218. static int sti_uniperiph_dai_create_ctrl(struct snd_soc_dai *dai)
  219. {
  220. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  221. struct uniperif *uni = priv->dai_data.uni;
  222. struct snd_kcontrol_new *ctrl;
  223. int i;
  224. if (!uni->num_ctrls)
  225. return 0;
  226. for (i = 0; i < uni->num_ctrls; i++) {
  227. /*
  228. * Several Control can have same name. Controls are indexed on
  229. * Uniperipheral instance ID
  230. */
  231. ctrl = &uni->snd_ctrls[i];
  232. ctrl->index = uni->id;
  233. ctrl->device = uni->id;
  234. }
  235. return snd_soc_add_dai_controls(dai, uni->snd_ctrls, uni->num_ctrls);
  236. }
  237. /*
  238. * DAI
  239. */
  240. int sti_uniperiph_dai_hw_params(struct snd_pcm_substream *substream,
  241. struct snd_pcm_hw_params *params,
  242. struct snd_soc_dai *dai)
  243. {
  244. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  245. struct uniperif *uni = priv->dai_data.uni;
  246. struct snd_dmaengine_dai_dma_data *dma_data;
  247. int transfer_size;
  248. if (uni->type == SND_ST_UNIPERIF_TYPE_TDM)
  249. /* transfer size = user frame size (in 32-bits FIFO cell) */
  250. transfer_size = snd_soc_params_to_frame_size(params) / 32;
  251. else
  252. transfer_size = params_channels(params) * UNIPERIF_FIFO_FRAMES;
  253. dma_data = snd_soc_dai_get_dma_data(dai, substream);
  254. dma_data->maxburst = transfer_size;
  255. return 0;
  256. }
  257. int sti_uniperiph_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  258. {
  259. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  260. priv->dai_data.uni->daifmt = fmt;
  261. return 0;
  262. }
  263. static int sti_uniperiph_suspend(struct snd_soc_component *component)
  264. {
  265. struct sti_uniperiph_data *priv = snd_soc_component_get_drvdata(component);
  266. struct uniperif *uni = priv->dai_data.uni;
  267. int ret;
  268. /* The uniperipheral should be in stopped state */
  269. if (uni->state != UNIPERIF_STATE_STOPPED) {
  270. dev_err(uni->dev, "%s: invalid uni state( %d)\n",
  271. __func__, (int)uni->state);
  272. return -EBUSY;
  273. }
  274. /* Pinctrl: switch pinstate to sleep */
  275. ret = pinctrl_pm_select_sleep_state(uni->dev);
  276. if (ret)
  277. dev_err(uni->dev, "%s: failed to select pinctrl state\n",
  278. __func__);
  279. return ret;
  280. }
  281. static int sti_uniperiph_resume(struct snd_soc_component *component)
  282. {
  283. struct sti_uniperiph_data *priv = snd_soc_component_get_drvdata(component);
  284. struct uniperif *uni = priv->dai_data.uni;
  285. int ret;
  286. if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) {
  287. ret = uni_player_resume(uni);
  288. if (ret)
  289. return ret;
  290. }
  291. /* pinctrl: switch pinstate to default */
  292. ret = pinctrl_pm_select_default_state(uni->dev);
  293. if (ret)
  294. dev_err(uni->dev, "%s: failed to select pinctrl state\n",
  295. __func__);
  296. return ret;
  297. }
  298. static int sti_uniperiph_dai_probe(struct snd_soc_dai *dai)
  299. {
  300. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  301. struct sti_uniperiph_dai *dai_data = &priv->dai_data;
  302. /* DMA settings*/
  303. if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK)
  304. snd_soc_dai_init_dma_data(dai, &dai_data->dma_data, NULL);
  305. else
  306. snd_soc_dai_init_dma_data(dai, NULL, &dai_data->dma_data);
  307. dai_data->dma_data.addr = dai_data->uni->fifo_phys_address;
  308. dai_data->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  309. return sti_uniperiph_dai_create_ctrl(dai);
  310. }
  311. static const struct snd_soc_dai_driver sti_uniperiph_dai_template = {
  312. .probe = sti_uniperiph_dai_probe,
  313. };
  314. static const struct snd_soc_component_driver sti_uniperiph_dai_component = {
  315. .name = "sti_cpu_dai",
  316. .suspend = sti_uniperiph_suspend,
  317. .resume = sti_uniperiph_resume,
  318. .legacy_dai_naming = 1,
  319. };
  320. static int sti_uniperiph_cpu_dai_of(struct device_node *node,
  321. struct sti_uniperiph_data *priv)
  322. {
  323. struct device *dev = &priv->pdev->dev;
  324. struct sti_uniperiph_dai *dai_data = &priv->dai_data;
  325. struct snd_soc_dai_driver *dai = priv->dai;
  326. struct snd_soc_pcm_stream *stream;
  327. struct uniperif *uni;
  328. const struct of_device_id *of_id;
  329. const struct sti_uniperiph_dev_data *dev_data;
  330. const char *mode;
  331. int ret;
  332. /* Populate data structure depending on compatibility */
  333. of_id = of_match_node(snd_soc_sti_match, node);
  334. if (!of_id->data) {
  335. dev_err(dev, "data associated to device is missing\n");
  336. return -EINVAL;
  337. }
  338. dev_data = (struct sti_uniperiph_dev_data *)of_id->data;
  339. uni = devm_kzalloc(dev, sizeof(*uni), GFP_KERNEL);
  340. if (!uni)
  341. return -ENOMEM;
  342. uni->id = dev_data->id;
  343. uni->ver = dev_data->version;
  344. *dai = sti_uniperiph_dai_template;
  345. dai->name = dev_data->dai_names;
  346. /* Get resources and base address */
  347. uni->base = devm_platform_get_and_ioremap_resource(priv->pdev, 0, &uni->mem_region);
  348. if (IS_ERR(uni->base))
  349. return PTR_ERR(uni->base);
  350. uni->fifo_phys_address = uni->mem_region->start +
  351. UNIPERIF_FIFO_DATA_OFFSET(uni);
  352. uni->irq = platform_get_irq(priv->pdev, 0);
  353. if (uni->irq < 0)
  354. return -ENXIO;
  355. uni->type = dev_data->type;
  356. /* check if player should be configured for tdm */
  357. if (dev_data->type & SND_ST_UNIPERIF_TYPE_TDM) {
  358. if (!of_property_read_string(node, "st,tdm-mode", &mode))
  359. uni->type = SND_ST_UNIPERIF_TYPE_TDM;
  360. else
  361. uni->type = SND_ST_UNIPERIF_TYPE_PCM;
  362. }
  363. dai_data->uni = uni;
  364. dai_data->stream = dev_data->stream;
  365. if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) {
  366. ret = uni_player_init(priv->pdev, uni);
  367. stream = &dai->playback;
  368. } else {
  369. ret = uni_reader_init(priv->pdev, uni);
  370. stream = &dai->capture;
  371. }
  372. if (ret < 0)
  373. return ret;
  374. dai->ops = uni->dai_ops;
  375. stream->stream_name = dai->name;
  376. stream->channels_min = uni->hw->channels_min;
  377. stream->channels_max = uni->hw->channels_max;
  378. stream->rates = uni->hw->rates;
  379. stream->formats = uni->hw->formats;
  380. return 0;
  381. }
  382. static const struct snd_dmaengine_pcm_config dmaengine_pcm_config = {
  383. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  384. };
  385. static int sti_uniperiph_probe(struct platform_device *pdev)
  386. {
  387. struct sti_uniperiph_data *priv;
  388. struct device_node *node = pdev->dev.of_node;
  389. int ret;
  390. /* Allocate the private data and the CPU_DAI array */
  391. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  392. if (!priv)
  393. return -ENOMEM;
  394. priv->dai = devm_kzalloc(&pdev->dev, sizeof(*priv->dai), GFP_KERNEL);
  395. if (!priv->dai)
  396. return -ENOMEM;
  397. priv->pdev = pdev;
  398. ret = sti_uniperiph_cpu_dai_of(node, priv);
  399. if (ret < 0)
  400. return ret;
  401. dev_set_drvdata(&pdev->dev, priv);
  402. ret = devm_snd_soc_register_component(&pdev->dev,
  403. &sti_uniperiph_dai_component,
  404. priv->dai, 1);
  405. if (ret < 0)
  406. return ret;
  407. return devm_snd_dmaengine_pcm_register(&pdev->dev,
  408. &dmaengine_pcm_config, 0);
  409. }
  410. static struct platform_driver sti_uniperiph_driver = {
  411. .driver = {
  412. .name = "sti-uniperiph-dai",
  413. .of_match_table = snd_soc_sti_match,
  414. },
  415. .probe = sti_uniperiph_probe,
  416. };
  417. module_platform_driver(sti_uniperiph_driver);
  418. MODULE_DESCRIPTION("uniperipheral DAI driver");
  419. MODULE_AUTHOR("Arnaud Pouliquen <[email protected]>");
  420. MODULE_LICENSE("GPL v2");