p1022_rdk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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.h>
  19. #include <linux/of_address.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. * @card: ASoC card instance
  74. *
  75. * This function is used to initialize the board-specific hardware.
  76. *
  77. * Here we program the DMACR and PMUXCR registers.
  78. *
  79. * Returns: %0 on success or negative errno value on error
  80. */
  81. static int p1022_rdk_machine_probe(struct snd_soc_card *card)
  82. {
  83. struct machine_data *mdata =
  84. container_of(card, struct machine_data, card);
  85. struct ccsr_guts __iomem *guts;
  86. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  87. if (!guts) {
  88. dev_err(card->dev, "could not map global utilities\n");
  89. return -ENOMEM;
  90. }
  91. /* Enable SSI Tx signal */
  92. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
  93. CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
  94. /* Enable SSI Rx signal */
  95. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
  96. CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
  97. /* Enable DMA Channel for SSI */
  98. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
  99. CCSR_GUTS_DMUXCR_SSI);
  100. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
  101. CCSR_GUTS_DMUXCR_SSI);
  102. iounmap(guts);
  103. return 0;
  104. }
  105. /**
  106. * p1022_rdk_startup - program the board with various hardware parameters
  107. * @substream: ASoC substream object
  108. *
  109. * This function takes board-specific information, like clock frequencies
  110. * and serial data formats, and passes that information to the codec and
  111. * transport drivers.
  112. *
  113. * Returns: %0 on success or negative errno value on error
  114. */
  115. static int p1022_rdk_startup(struct snd_pcm_substream *substream)
  116. {
  117. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  118. struct machine_data *mdata =
  119. container_of(rtd->card, struct machine_data, card);
  120. struct device *dev = rtd->card->dev;
  121. int ret = 0;
  122. /* Tell the codec driver what the serial protocol is. */
  123. ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_codec(rtd, 0), mdata->dai_format);
  124. if (ret < 0) {
  125. dev_err(dev, "could not set codec driver audio format (ret=%i)\n",
  126. ret);
  127. return ret;
  128. }
  129. ret = snd_soc_dai_set_pll(snd_soc_rtd_to_codec(rtd, 0), 0, 0, mdata->clk_frequency,
  130. mdata->clk_frequency);
  131. if (ret < 0) {
  132. dev_err(dev, "could not set codec PLL frequency (ret=%i)\n",
  133. ret);
  134. return ret;
  135. }
  136. return 0;
  137. }
  138. /**
  139. * p1022_rdk_machine_remove - Remove the sound device
  140. * @card: ASoC card instance
  141. *
  142. * This function is called to remove the sound device for one SSI. We
  143. * de-program the DMACR and PMUXCR register.
  144. *
  145. * Returns: %0 on success or negative errno value on error
  146. */
  147. static int p1022_rdk_machine_remove(struct snd_soc_card *card)
  148. {
  149. struct machine_data *mdata =
  150. container_of(card, struct machine_data, card);
  151. struct ccsr_guts __iomem *guts;
  152. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  153. if (!guts) {
  154. dev_err(card->dev, "could not map global utilities\n");
  155. return -ENOMEM;
  156. }
  157. /* Restore the signal routing */
  158. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
  159. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
  160. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
  161. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
  162. iounmap(guts);
  163. return 0;
  164. }
  165. /*
  166. * p1022_rdk_ops: ASoC machine driver operations
  167. */
  168. static const struct snd_soc_ops p1022_rdk_ops = {
  169. .startup = p1022_rdk_startup,
  170. };
  171. /**
  172. * p1022_rdk_probe - platform probe function for the machine driver
  173. * @pdev: platform device pointer
  174. *
  175. * Although this is a machine driver, the SSI node is the "master" node with
  176. * respect to audio hardware connections. Therefore, we create a new ASoC
  177. * device for each new SSI node that has a codec attached.
  178. *
  179. * Returns: %0 on success or negative errno value on error
  180. */
  181. static int p1022_rdk_probe(struct platform_device *pdev)
  182. {
  183. struct device *dev = pdev->dev.parent;
  184. /* ssi_pdev is the platform device for the SSI node that probed us */
  185. struct platform_device *ssi_pdev = to_platform_device(dev);
  186. struct device_node *np = ssi_pdev->dev.of_node;
  187. struct device_node *codec_np = NULL;
  188. struct machine_data *mdata;
  189. struct snd_soc_dai_link_component *comp;
  190. const u32 *iprop;
  191. int ret;
  192. /* Find the codec node for this SSI. */
  193. codec_np = of_parse_phandle(np, "codec-handle", 0);
  194. if (!codec_np) {
  195. dev_err(dev, "could not find codec node\n");
  196. return -EINVAL;
  197. }
  198. mdata = kzalloc_obj(struct machine_data);
  199. if (!mdata) {
  200. ret = -ENOMEM;
  201. goto error_put;
  202. }
  203. comp = devm_kzalloc(&pdev->dev, 6 * sizeof(*comp), GFP_KERNEL);
  204. if (!comp) {
  205. ret = -ENOMEM;
  206. goto error_put;
  207. }
  208. mdata->dai[0].cpus = &comp[0];
  209. mdata->dai[0].codecs = &comp[1];
  210. mdata->dai[0].platforms = &comp[2];
  211. mdata->dai[0].num_cpus = 1;
  212. mdata->dai[0].num_codecs = 1;
  213. mdata->dai[0].num_platforms = 1;
  214. mdata->dai[1].cpus = &comp[3];
  215. mdata->dai[1].codecs = &comp[4];
  216. mdata->dai[1].platforms = &comp[5];
  217. mdata->dai[1].num_cpus = 1;
  218. mdata->dai[1].num_codecs = 1;
  219. mdata->dai[1].num_platforms = 1;
  220. mdata->dai[0].cpus->dai_name = dev_name(&ssi_pdev->dev);
  221. mdata->dai[0].ops = &p1022_rdk_ops;
  222. /* ASoC core can match codec with device node */
  223. mdata->dai[0].codecs->of_node = codec_np;
  224. /*
  225. * We register two DAIs per SSI, one for playback and the other for
  226. * capture. We support codecs that have separate DAIs for both playback
  227. * and capture.
  228. */
  229. memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
  230. /* The DAI names from the codec (snd_soc_dai_driver.name) */
  231. mdata->dai[0].codecs->dai_name = "wm8960-hifi";
  232. mdata->dai[1].codecs->dai_name = mdata->dai[0].codecs->dai_name;
  233. /*
  234. * Configure the SSI for I2S slave mode. Older device trees have
  235. * an fsl,mode property, but we ignore that since there's really
  236. * only one way to configure the SSI.
  237. */
  238. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  239. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBP_CFP;
  240. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  241. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  242. /*
  243. * In i2s-slave mode, the codec has its own clock source, so we
  244. * need to get the frequency from the device tree and pass it to
  245. * the codec driver.
  246. */
  247. iprop = of_get_property(codec_np, "clock-frequency", NULL);
  248. if (!iprop || !*iprop) {
  249. dev_err(&pdev->dev, "codec bus-frequency property is missing or invalid\n");
  250. ret = -EINVAL;
  251. goto error;
  252. }
  253. mdata->clk_frequency = be32_to_cpup(iprop);
  254. if (!mdata->clk_frequency) {
  255. dev_err(&pdev->dev, "unknown clock frequency\n");
  256. ret = -EINVAL;
  257. goto error;
  258. }
  259. /* Find the playback DMA channel to use. */
  260. mdata->dai[0].platforms->name = mdata->platform_name[0];
  261. ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
  262. &mdata->dma_channel_id[0],
  263. &mdata->dma_id[0]);
  264. if (ret) {
  265. dev_err(&pdev->dev, "missing/invalid playback DMA phandle (ret=%i)\n",
  266. ret);
  267. goto error;
  268. }
  269. /* Find the capture DMA channel to use. */
  270. mdata->dai[1].platforms->name = mdata->platform_name[1];
  271. ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
  272. &mdata->dma_channel_id[1],
  273. &mdata->dma_id[1]);
  274. if (ret) {
  275. dev_err(&pdev->dev, "missing/invalid capture DMA phandle (ret=%i)\n",
  276. ret);
  277. goto error;
  278. }
  279. /* Initialize our DAI data structure. */
  280. mdata->dai[0].stream_name = "playback";
  281. mdata->dai[1].stream_name = "capture";
  282. mdata->dai[0].name = mdata->dai[0].stream_name;
  283. mdata->dai[1].name = mdata->dai[1].stream_name;
  284. mdata->card.probe = p1022_rdk_machine_probe;
  285. mdata->card.remove = p1022_rdk_machine_remove;
  286. mdata->card.name = pdev->name; /* The platform driver name */
  287. mdata->card.owner = THIS_MODULE;
  288. mdata->card.dev = &pdev->dev;
  289. mdata->card.num_links = 2;
  290. mdata->card.dai_link = mdata->dai;
  291. /* Register with ASoC */
  292. ret = snd_soc_register_card(&mdata->card);
  293. if (ret) {
  294. dev_err(&pdev->dev, "could not register card (ret=%i)\n", ret);
  295. goto error;
  296. }
  297. return 0;
  298. error:
  299. kfree(mdata);
  300. error_put:
  301. of_node_put(codec_np);
  302. return ret;
  303. }
  304. /**
  305. * p1022_rdk_remove - remove the platform device
  306. * @pdev: platform device pointer
  307. *
  308. * This function is called when the platform device is removed.
  309. */
  310. static void p1022_rdk_remove(struct platform_device *pdev)
  311. {
  312. struct snd_soc_card *card = platform_get_drvdata(pdev);
  313. struct machine_data *mdata =
  314. container_of(card, struct machine_data, card);
  315. snd_soc_unregister_card(card);
  316. kfree(mdata);
  317. }
  318. static struct platform_driver p1022_rdk_driver = {
  319. .probe = p1022_rdk_probe,
  320. .remove = p1022_rdk_remove,
  321. .driver = {
  322. /*
  323. * The name must match 'compatible' property in the device tree,
  324. * in lowercase letters.
  325. */
  326. .name = "snd-soc-p1022rdk",
  327. },
  328. };
  329. /**
  330. * p1022_rdk_init - machine driver initialization.
  331. *
  332. * This function is called when this module is loaded.
  333. *
  334. * Returns: %0 on success or negative errno value on error
  335. */
  336. static int __init p1022_rdk_init(void)
  337. {
  338. struct device_node *guts_np;
  339. struct resource res;
  340. /* Get the physical address of the global utilities registers */
  341. guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
  342. if (of_address_to_resource(guts_np, 0, &res)) {
  343. pr_err("snd-soc-p1022rdk: missing/invalid global utils node\n");
  344. of_node_put(guts_np);
  345. return -EINVAL;
  346. }
  347. guts_phys = res.start;
  348. of_node_put(guts_np);
  349. return platform_driver_register(&p1022_rdk_driver);
  350. }
  351. /**
  352. * p1022_rdk_exit - machine driver exit
  353. *
  354. * This function is called when this driver is unloaded.
  355. */
  356. static void __exit p1022_rdk_exit(void)
  357. {
  358. platform_driver_unregister(&p1022_rdk_driver);
  359. }
  360. late_initcall(p1022_rdk_init);
  361. module_exit(p1022_rdk_exit);
  362. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  363. MODULE_DESCRIPTION("Freescale / iVeia P1022 RDK ALSA SoC machine driver");
  364. MODULE_LICENSE("GPL v2");