p1022_rdk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Freescale P1022RDK ALSA SoC Machine driver
  4. //
  5. // Author: Timur Tabi <timur@freescale.com>
  6. //
  7. // Copyright 2012 Freescale Semiconductor, Inc.
  8. //
  9. // Note: in order for audio to work correctly, the output controls need
  10. // to be enabled, because they control the clock. So for playback, for
  11. // example:
  12. //
  13. // amixer sset 'Left Output Mixer PCM' on
  14. // amixer sset 'Right Output Mixer PCM' on
  15. #include <linux/module.h>
  16. #include <linux/fsl/guts.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_device.h>
  20. #include <linux/slab.h>
  21. #include <sound/soc.h>
  22. #include "fsl_dma.h"
  23. #include "fsl_ssi.h"
  24. #include "fsl_utils.h"
  25. /* P1022-specific PMUXCR and DMUXCR bit definitions */
  26. #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
  27. #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
  28. #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
  29. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
  30. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
  31. #define CCSR_GUTS_DMUXCR_PAD 1 /* DMA controller/channel set to pad */
  32. #define CCSR_GUTS_DMUXCR_SSI 2 /* DMA controller/channel set to SSI */
  33. /*
  34. * Set the DMACR register in the GUTS
  35. *
  36. * The DMACR register determines the source of initiated transfers for each
  37. * channel on each DMA controller. Rather than have a bunch of repetitive
  38. * macros for the bit patterns, we just have a function that calculates
  39. * them.
  40. *
  41. * guts: Pointer to GUTS structure
  42. * co: The DMA controller (0 or 1)
  43. * ch: The channel on the DMA controller (0, 1, 2, or 3)
  44. * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
  45. */
  46. static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
  47. unsigned int co, unsigned int ch, unsigned int device)
  48. {
  49. unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
  50. clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
  51. }
  52. /* There's only one global utilities register */
  53. static phys_addr_t guts_phys;
  54. /**
  55. * machine_data: machine-specific ASoC device data
  56. *
  57. * This structure contains data for a single sound platform device on an
  58. * P1022 RDK. Some of the data is taken from the device tree.
  59. */
  60. struct machine_data {
  61. struct snd_soc_dai_link dai[2];
  62. struct snd_soc_card card;
  63. unsigned int dai_format;
  64. unsigned int codec_clk_direction;
  65. unsigned int cpu_clk_direction;
  66. unsigned int clk_frequency;
  67. unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
  68. unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
  69. char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
  70. };
  71. /**
  72. * p1022_rdk_machine_probe: initialize the board
  73. *
  74. * This function is used to initialize the board-specific hardware.
  75. *
  76. * Here we program the DMACR and PMUXCR registers.
  77. */
  78. static int p1022_rdk_machine_probe(struct snd_soc_card *card)
  79. {
  80. struct machine_data *mdata =
  81. container_of(card, struct machine_data, card);
  82. struct ccsr_guts __iomem *guts;
  83. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  84. if (!guts) {
  85. dev_err(card->dev, "could not map global utilities\n");
  86. return -ENOMEM;
  87. }
  88. /* Enable SSI Tx signal */
  89. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
  90. CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
  91. /* Enable SSI Rx signal */
  92. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
  93. CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
  94. /* Enable DMA Channel for SSI */
  95. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
  96. CCSR_GUTS_DMUXCR_SSI);
  97. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
  98. CCSR_GUTS_DMUXCR_SSI);
  99. iounmap(guts);
  100. return 0;
  101. }
  102. /**
  103. * p1022_rdk_startup: program the board with various hardware parameters
  104. *
  105. * This function takes board-specific information, like clock frequencies
  106. * and serial data formats, and passes that information to the codec and
  107. * transport drivers.
  108. */
  109. static int p1022_rdk_startup(struct snd_pcm_substream *substream)
  110. {
  111. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  112. struct machine_data *mdata =
  113. container_of(rtd->card, struct machine_data, card);
  114. struct device *dev = rtd->card->dev;
  115. int ret = 0;
  116. /* Tell the codec driver what the serial protocol is. */
  117. ret = snd_soc_dai_set_fmt(asoc_rtd_to_codec(rtd, 0), mdata->dai_format);
  118. if (ret < 0) {
  119. dev_err(dev, "could not set codec driver audio format (ret=%i)\n",
  120. ret);
  121. return ret;
  122. }
  123. ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0), 0, 0, mdata->clk_frequency,
  124. mdata->clk_frequency);
  125. if (ret < 0) {
  126. dev_err(dev, "could not set codec PLL frequency (ret=%i)\n",
  127. ret);
  128. return ret;
  129. }
  130. return 0;
  131. }
  132. /**
  133. * p1022_rdk_machine_remove: Remove the sound device
  134. *
  135. * This function is called to remove the sound device for one SSI. We
  136. * de-program the DMACR and PMUXCR register.
  137. */
  138. static int p1022_rdk_machine_remove(struct snd_soc_card *card)
  139. {
  140. struct machine_data *mdata =
  141. container_of(card, struct machine_data, card);
  142. struct ccsr_guts __iomem *guts;
  143. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  144. if (!guts) {
  145. dev_err(card->dev, "could not map global utilities\n");
  146. return -ENOMEM;
  147. }
  148. /* Restore the signal routing */
  149. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
  150. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
  151. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
  152. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
  153. iounmap(guts);
  154. return 0;
  155. }
  156. /**
  157. * p1022_rdk_ops: ASoC machine driver operations
  158. */
  159. static const struct snd_soc_ops p1022_rdk_ops = {
  160. .startup = p1022_rdk_startup,
  161. };
  162. /**
  163. * p1022_rdk_probe: platform probe function for the machine driver
  164. *
  165. * Although this is a machine driver, the SSI node is the "master" node with
  166. * respect to audio hardware connections. Therefore, we create a new ASoC
  167. * device for each new SSI node that has a codec attached.
  168. */
  169. static int p1022_rdk_probe(struct platform_device *pdev)
  170. {
  171. struct device *dev = pdev->dev.parent;
  172. /* ssi_pdev is the platform device for the SSI node that probed us */
  173. struct platform_device *ssi_pdev = to_platform_device(dev);
  174. struct device_node *np = ssi_pdev->dev.of_node;
  175. struct device_node *codec_np = NULL;
  176. struct machine_data *mdata;
  177. struct snd_soc_dai_link_component *comp;
  178. const u32 *iprop;
  179. int ret;
  180. /* Find the codec node for this SSI. */
  181. codec_np = of_parse_phandle(np, "codec-handle", 0);
  182. if (!codec_np) {
  183. dev_err(dev, "could not find codec node\n");
  184. return -EINVAL;
  185. }
  186. mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
  187. if (!mdata) {
  188. ret = -ENOMEM;
  189. goto error_put;
  190. }
  191. comp = devm_kzalloc(&pdev->dev, 6 * sizeof(*comp), GFP_KERNEL);
  192. if (!comp) {
  193. ret = -ENOMEM;
  194. goto error_put;
  195. }
  196. mdata->dai[0].cpus = &comp[0];
  197. mdata->dai[0].codecs = &comp[1];
  198. mdata->dai[0].platforms = &comp[2];
  199. mdata->dai[0].num_cpus = 1;
  200. mdata->dai[0].num_codecs = 1;
  201. mdata->dai[0].num_platforms = 1;
  202. mdata->dai[1].cpus = &comp[3];
  203. mdata->dai[1].codecs = &comp[4];
  204. mdata->dai[1].platforms = &comp[5];
  205. mdata->dai[1].num_cpus = 1;
  206. mdata->dai[1].num_codecs = 1;
  207. mdata->dai[1].num_platforms = 1;
  208. mdata->dai[0].cpus->dai_name = dev_name(&ssi_pdev->dev);
  209. mdata->dai[0].ops = &p1022_rdk_ops;
  210. /* ASoC core can match codec with device node */
  211. mdata->dai[0].codecs->of_node = codec_np;
  212. /*
  213. * We register two DAIs per SSI, one for playback and the other for
  214. * capture. We support codecs that have separate DAIs for both playback
  215. * and capture.
  216. */
  217. memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
  218. /* The DAI names from the codec (snd_soc_dai_driver.name) */
  219. mdata->dai[0].codecs->dai_name = "wm8960-hifi";
  220. mdata->dai[1].codecs->dai_name = mdata->dai[0].codecs->dai_name;
  221. /*
  222. * Configure the SSI for I2S slave mode. Older device trees have
  223. * an fsl,mode property, but we ignore that since there's really
  224. * only one way to configure the SSI.
  225. */
  226. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  227. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
  228. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  229. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  230. /*
  231. * In i2s-slave mode, the codec has its own clock source, so we
  232. * need to get the frequency from the device tree and pass it to
  233. * the codec driver.
  234. */
  235. iprop = of_get_property(codec_np, "clock-frequency", NULL);
  236. if (!iprop || !*iprop) {
  237. dev_err(&pdev->dev, "codec bus-frequency property is missing or invalid\n");
  238. ret = -EINVAL;
  239. goto error;
  240. }
  241. mdata->clk_frequency = be32_to_cpup(iprop);
  242. if (!mdata->clk_frequency) {
  243. dev_err(&pdev->dev, "unknown clock frequency\n");
  244. ret = -EINVAL;
  245. goto error;
  246. }
  247. /* Find the playback DMA channel to use. */
  248. mdata->dai[0].platforms->name = mdata->platform_name[0];
  249. ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
  250. &mdata->dma_channel_id[0],
  251. &mdata->dma_id[0]);
  252. if (ret) {
  253. dev_err(&pdev->dev, "missing/invalid playback DMA phandle (ret=%i)\n",
  254. ret);
  255. goto error;
  256. }
  257. /* Find the capture DMA channel to use. */
  258. mdata->dai[1].platforms->name = mdata->platform_name[1];
  259. ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
  260. &mdata->dma_channel_id[1],
  261. &mdata->dma_id[1]);
  262. if (ret) {
  263. dev_err(&pdev->dev, "missing/invalid capture DMA phandle (ret=%i)\n",
  264. ret);
  265. goto error;
  266. }
  267. /* Initialize our DAI data structure. */
  268. mdata->dai[0].stream_name = "playback";
  269. mdata->dai[1].stream_name = "capture";
  270. mdata->dai[0].name = mdata->dai[0].stream_name;
  271. mdata->dai[1].name = mdata->dai[1].stream_name;
  272. mdata->card.probe = p1022_rdk_machine_probe;
  273. mdata->card.remove = p1022_rdk_machine_remove;
  274. mdata->card.name = pdev->name; /* The platform driver name */
  275. mdata->card.owner = THIS_MODULE;
  276. mdata->card.dev = &pdev->dev;
  277. mdata->card.num_links = 2;
  278. mdata->card.dai_link = mdata->dai;
  279. /* Register with ASoC */
  280. ret = snd_soc_register_card(&mdata->card);
  281. if (ret) {
  282. dev_err(&pdev->dev, "could not register card (ret=%i)\n", ret);
  283. goto error;
  284. }
  285. return 0;
  286. error:
  287. kfree(mdata);
  288. error_put:
  289. of_node_put(codec_np);
  290. return ret;
  291. }
  292. /**
  293. * p1022_rdk_remove: remove the platform device
  294. *
  295. * This function is called when the platform device is removed.
  296. */
  297. static int p1022_rdk_remove(struct platform_device *pdev)
  298. {
  299. struct snd_soc_card *card = platform_get_drvdata(pdev);
  300. struct machine_data *mdata =
  301. container_of(card, struct machine_data, card);
  302. snd_soc_unregister_card(card);
  303. kfree(mdata);
  304. return 0;
  305. }
  306. static struct platform_driver p1022_rdk_driver = {
  307. .probe = p1022_rdk_probe,
  308. .remove = p1022_rdk_remove,
  309. .driver = {
  310. /*
  311. * The name must match 'compatible' property in the device tree,
  312. * in lowercase letters.
  313. */
  314. .name = "snd-soc-p1022rdk",
  315. },
  316. };
  317. /**
  318. * p1022_rdk_init: machine driver initialization.
  319. *
  320. * This function is called when this module is loaded.
  321. */
  322. static int __init p1022_rdk_init(void)
  323. {
  324. struct device_node *guts_np;
  325. struct resource res;
  326. /* Get the physical address of the global utilities registers */
  327. guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
  328. if (of_address_to_resource(guts_np, 0, &res)) {
  329. pr_err("snd-soc-p1022rdk: missing/invalid global utils node\n");
  330. of_node_put(guts_np);
  331. return -EINVAL;
  332. }
  333. guts_phys = res.start;
  334. of_node_put(guts_np);
  335. return platform_driver_register(&p1022_rdk_driver);
  336. }
  337. /**
  338. * p1022_rdk_exit: machine driver exit
  339. *
  340. * This function is called when this driver is unloaded.
  341. */
  342. static void __exit p1022_rdk_exit(void)
  343. {
  344. platform_driver_unregister(&p1022_rdk_driver);
  345. }
  346. late_initcall(p1022_rdk_init);
  347. module_exit(p1022_rdk_exit);
  348. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  349. MODULE_DESCRIPTION("Freescale / iVeia P1022 RDK ALSA SoC machine driver");
  350. MODULE_LICENSE("GPL v2");