sti_uniperif.c 13 KB

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