p1022_rdk.c 11 KB

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