sti_uniperif.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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/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. int sti_uniperiph_reset(struct uniperif *uni)
  90. {
  91. int count = 10;
  92. /* Reset uniperipheral uni */
  93. SET_UNIPERIF_SOFT_RST_SOFT_RST(uni);
  94. if (uni->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
  95. while (GET_UNIPERIF_SOFT_RST_SOFT_RST(uni) && count) {
  96. udelay(5);
  97. count--;
  98. }
  99. }
  100. if (!count) {
  101. dev_err(uni->dev, "Failed to reset uniperif\n");
  102. return -EIO;
  103. }
  104. return 0;
  105. }
  106. int sti_uniperiph_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  107. unsigned int rx_mask, int slots,
  108. int slot_width)
  109. {
  110. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  111. struct uniperif *uni = priv->dai_data.uni;
  112. int i, frame_size, avail_slots;
  113. if (!UNIPERIF_TYPE_IS_TDM(uni)) {
  114. dev_err(uni->dev, "cpu dai not in tdm mode\n");
  115. return -EINVAL;
  116. }
  117. /* store info in unip context */
  118. uni->tdm_slot.slots = slots;
  119. uni->tdm_slot.slot_width = slot_width;
  120. /* unip is unidirectionnal */
  121. uni->tdm_slot.mask = (tx_mask != 0) ? tx_mask : rx_mask;
  122. /* number of available timeslots */
  123. for (i = 0, avail_slots = 0; i < uni->tdm_slot.slots; i++) {
  124. if ((uni->tdm_slot.mask >> i) & 0x01)
  125. avail_slots++;
  126. }
  127. uni->tdm_slot.avail_slots = avail_slots;
  128. /* frame size in bytes */
  129. frame_size = uni->tdm_slot.avail_slots * uni->tdm_slot.slot_width / 8;
  130. /* check frame size is allowed */
  131. if ((frame_size > UNIPERIF_MAX_FRAME_SZ) ||
  132. (frame_size & ~(int)UNIPERIF_ALLOWED_FRAME_SZ)) {
  133. dev_err(uni->dev, "frame size not allowed: %d bytes\n",
  134. frame_size);
  135. return -EINVAL;
  136. }
  137. return 0;
  138. }
  139. int sti_uniperiph_fix_tdm_chan(struct snd_pcm_hw_params *params,
  140. struct snd_pcm_hw_rule *rule)
  141. {
  142. struct uniperif *uni = rule->private;
  143. struct snd_interval t;
  144. t.min = uni->tdm_slot.avail_slots;
  145. t.max = uni->tdm_slot.avail_slots;
  146. t.openmin = 0;
  147. t.openmax = 0;
  148. t.integer = 0;
  149. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  150. }
  151. int sti_uniperiph_fix_tdm_format(struct snd_pcm_hw_params *params,
  152. struct snd_pcm_hw_rule *rule)
  153. {
  154. struct uniperif *uni = rule->private;
  155. struct snd_mask *maskp = hw_param_mask(params, rule->var);
  156. u64 format;
  157. switch (uni->tdm_slot.slot_width) {
  158. case 16:
  159. format = SNDRV_PCM_FMTBIT_S16_LE;
  160. break;
  161. case 32:
  162. format = SNDRV_PCM_FMTBIT_S32_LE;
  163. break;
  164. default:
  165. dev_err(uni->dev, "format not supported: %d bits\n",
  166. uni->tdm_slot.slot_width);
  167. return -EINVAL;
  168. }
  169. maskp->bits[0] &= (u_int32_t)format;
  170. maskp->bits[1] &= (u_int32_t)(format >> 32);
  171. /* clear remaining indexes */
  172. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX - 64) / 8);
  173. if (!maskp->bits[0] && !maskp->bits[1])
  174. return -EINVAL;
  175. return 0;
  176. }
  177. int sti_uniperiph_get_tdm_word_pos(struct uniperif *uni,
  178. unsigned int *word_pos)
  179. {
  180. int slot_width = uni->tdm_slot.slot_width / 8;
  181. int slots_num = uni->tdm_slot.slots;
  182. unsigned int slots_mask = uni->tdm_slot.mask;
  183. int i, j, k;
  184. unsigned int word16_pos[4];
  185. /* word16_pos:
  186. * word16_pos[0] = WORDX_LSB
  187. * word16_pos[1] = WORDX_MSB,
  188. * word16_pos[2] = WORDX+1_LSB
  189. * word16_pos[3] = WORDX+1_MSB
  190. */
  191. /* set unip word position */
  192. for (i = 0, j = 0, k = 0; (i < slots_num) && (k < WORD_MAX); i++) {
  193. if ((slots_mask >> i) & 0x01) {
  194. word16_pos[j] = i * slot_width;
  195. if (slot_width == 4) {
  196. word16_pos[j + 1] = word16_pos[j] + 2;
  197. j++;
  198. }
  199. j++;
  200. if (j > 3) {
  201. word_pos[k] = word16_pos[1] |
  202. (word16_pos[0] << 8) |
  203. (word16_pos[3] << 16) |
  204. (word16_pos[2] << 24);
  205. j = 0;
  206. k++;
  207. }
  208. }
  209. }
  210. return 0;
  211. }
  212. /*
  213. * sti_uniperiph_dai_create_ctrl
  214. * This function is used to create Ctrl associated to DAI but also pcm device.
  215. * Request is done by front end to associate ctrl with pcm device id
  216. */
  217. static int sti_uniperiph_dai_create_ctrl(struct snd_soc_dai *dai)
  218. {
  219. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  220. struct uniperif *uni = priv->dai_data.uni;
  221. struct snd_kcontrol_new *ctrl;
  222. int i;
  223. if (!uni->num_ctrls)
  224. return 0;
  225. for (i = 0; i < uni->num_ctrls; i++) {
  226. /*
  227. * Several Control can have same name. Controls are indexed on
  228. * Uniperipheral instance ID
  229. */
  230. ctrl = &uni->snd_ctrls[i];
  231. ctrl->index = uni->id;
  232. ctrl->device = uni->id;
  233. }
  234. return snd_soc_add_dai_controls(dai, uni->snd_ctrls, uni->num_ctrls);
  235. }
  236. /*
  237. * DAI
  238. */
  239. int sti_uniperiph_dai_hw_params(struct snd_pcm_substream *substream,
  240. struct snd_pcm_hw_params *params,
  241. struct snd_soc_dai *dai)
  242. {
  243. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  244. struct uniperif *uni = priv->dai_data.uni;
  245. struct snd_dmaengine_dai_dma_data *dma_data;
  246. int transfer_size;
  247. if (uni->type == SND_ST_UNIPERIF_TYPE_TDM)
  248. /* transfer size = user frame size (in 32-bits FIFO cell) */
  249. transfer_size = snd_soc_params_to_frame_size(params) / 32;
  250. else
  251. transfer_size = params_channels(params) * UNIPERIF_FIFO_FRAMES;
  252. dma_data = snd_soc_dai_get_dma_data(dai, substream);
  253. dma_data->maxburst = transfer_size;
  254. return 0;
  255. }
  256. int sti_uniperiph_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  257. {
  258. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  259. priv->dai_data.uni->daifmt = fmt;
  260. return 0;
  261. }
  262. static int sti_uniperiph_dai_suspend(struct snd_soc_dai *dai)
  263. {
  264. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  265. struct uniperif *uni = priv->dai_data.uni;
  266. int ret;
  267. /* The uniperipheral should be in stopped state */
  268. if (uni->state != UNIPERIF_STATE_STOPPED) {
  269. dev_err(uni->dev, "%s: invalid uni state( %d)\n",
  270. __func__, (int)uni->state);
  271. return -EBUSY;
  272. }
  273. /* Pinctrl: switch pinstate to sleep */
  274. ret = pinctrl_pm_select_sleep_state(uni->dev);
  275. if (ret)
  276. dev_err(uni->dev, "%s: failed to select pinctrl state\n",
  277. __func__);
  278. return ret;
  279. }
  280. static int sti_uniperiph_dai_resume(struct snd_soc_dai *dai)
  281. {
  282. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  283. struct uniperif *uni = priv->dai_data.uni;
  284. int ret;
  285. if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) {
  286. ret = uni_player_resume(uni);
  287. if (ret)
  288. return ret;
  289. }
  290. /* pinctrl: switch pinstate to default */
  291. ret = pinctrl_pm_select_default_state(uni->dev);
  292. if (ret)
  293. dev_err(uni->dev, "%s: failed to select pinctrl state\n",
  294. __func__);
  295. return ret;
  296. }
  297. static int sti_uniperiph_dai_probe(struct snd_soc_dai *dai)
  298. {
  299. struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
  300. struct sti_uniperiph_dai *dai_data = &priv->dai_data;
  301. /* DMA settings*/
  302. if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK)
  303. snd_soc_dai_init_dma_data(dai, &dai_data->dma_data, NULL);
  304. else
  305. snd_soc_dai_init_dma_data(dai, NULL, &dai_data->dma_data);
  306. dai_data->dma_data.addr = dai_data->uni->fifo_phys_address;
  307. dai_data->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  308. return sti_uniperiph_dai_create_ctrl(dai);
  309. }
  310. static const struct snd_soc_dai_driver sti_uniperiph_dai_template = {
  311. .probe = sti_uniperiph_dai_probe,
  312. .suspend = sti_uniperiph_dai_suspend,
  313. .resume = sti_uniperiph_dai_resume
  314. };
  315. static const struct snd_soc_component_driver sti_uniperiph_dai_component = {
  316. .name = "sti_cpu_dai",
  317. };
  318. static int sti_uniperiph_cpu_dai_of(struct device_node *node,
  319. struct sti_uniperiph_data *priv)
  320. {
  321. struct device *dev = &priv->pdev->dev;
  322. struct sti_uniperiph_dai *dai_data = &priv->dai_data;
  323. struct snd_soc_dai_driver *dai = priv->dai;
  324. struct snd_soc_pcm_stream *stream;
  325. struct uniperif *uni;
  326. const struct of_device_id *of_id;
  327. const struct sti_uniperiph_dev_data *dev_data;
  328. const char *mode;
  329. int ret;
  330. /* Populate data structure depending on compatibility */
  331. of_id = of_match_node(snd_soc_sti_match, node);
  332. if (!of_id->data) {
  333. dev_err(dev, "data associated to device is missing\n");
  334. return -EINVAL;
  335. }
  336. dev_data = (struct sti_uniperiph_dev_data *)of_id->data;
  337. uni = devm_kzalloc(dev, sizeof(*uni), GFP_KERNEL);
  338. if (!uni)
  339. return -ENOMEM;
  340. uni->id = dev_data->id;
  341. uni->ver = dev_data->version;
  342. *dai = sti_uniperiph_dai_template;
  343. dai->name = dev_data->dai_names;
  344. /* Get resources */
  345. uni->mem_region = platform_get_resource(priv->pdev, IORESOURCE_MEM, 0);
  346. if (!uni->mem_region) {
  347. dev_err(dev, "Failed to get memory resource\n");
  348. return -ENODEV;
  349. }
  350. uni->base = devm_ioremap_resource(dev, uni->mem_region);
  351. if (IS_ERR(uni->base))
  352. return PTR_ERR(uni->base);
  353. uni->fifo_phys_address = uni->mem_region->start +
  354. UNIPERIF_FIFO_DATA_OFFSET(uni);
  355. uni->irq = platform_get_irq(priv->pdev, 0);
  356. if (uni->irq < 0) {
  357. dev_err(dev, "Failed to get IRQ resource\n");
  358. return -ENXIO;
  359. }
  360. uni->type = dev_data->type;
  361. /* check if player should be configured for tdm */
  362. if (dev_data->type & SND_ST_UNIPERIF_TYPE_TDM) {
  363. if (!of_property_read_string(node, "st,tdm-mode", &mode))
  364. uni->type = SND_ST_UNIPERIF_TYPE_TDM;
  365. else
  366. uni->type = SND_ST_UNIPERIF_TYPE_PCM;
  367. }
  368. dai_data->uni = uni;
  369. dai_data->stream = dev_data->stream;
  370. if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) {
  371. ret = uni_player_init(priv->pdev, uni);
  372. stream = &dai->playback;
  373. } else {
  374. ret = uni_reader_init(priv->pdev, uni);
  375. stream = &dai->capture;
  376. }
  377. if (ret < 0)
  378. return ret;
  379. dai->ops = uni->dai_ops;
  380. stream->stream_name = dai->name;
  381. stream->channels_min = uni->hw->channels_min;
  382. stream->channels_max = uni->hw->channels_max;
  383. stream->rates = uni->hw->rates;
  384. stream->formats = uni->hw->formats;
  385. return 0;
  386. }
  387. static const struct snd_dmaengine_pcm_config dmaengine_pcm_config = {
  388. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  389. };
  390. static int sti_uniperiph_probe(struct platform_device *pdev)
  391. {
  392. struct sti_uniperiph_data *priv;
  393. struct device_node *node = pdev->dev.of_node;
  394. int ret;
  395. /* Allocate the private data and the CPU_DAI array */
  396. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  397. if (!priv)
  398. return -ENOMEM;
  399. priv->dai = devm_kzalloc(&pdev->dev, sizeof(*priv->dai), GFP_KERNEL);
  400. if (!priv->dai)
  401. return -ENOMEM;
  402. priv->pdev = pdev;
  403. ret = sti_uniperiph_cpu_dai_of(node, priv);
  404. dev_set_drvdata(&pdev->dev, priv);
  405. ret = devm_snd_soc_register_component(&pdev->dev,
  406. &sti_uniperiph_dai_component,
  407. priv->dai, 1);
  408. if (ret < 0)
  409. return ret;
  410. return devm_snd_dmaengine_pcm_register(&pdev->dev,
  411. &dmaengine_pcm_config, 0);
  412. }
  413. static struct platform_driver sti_uniperiph_driver = {
  414. .driver = {
  415. .name = "sti-uniperiph-dai",
  416. .of_match_table = snd_soc_sti_match,
  417. },
  418. .probe = sti_uniperiph_probe,
  419. };
  420. module_platform_driver(sti_uniperiph_driver);
  421. MODULE_DESCRIPTION("uniperipheral DAI driver");
  422. MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
  423. MODULE_LICENSE("GPL v2");