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/fsl/guts.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_device.h>
  24. #include <linux/slab.h>
  25. #include <sound/soc.h>
  26. #include "fsl_dma.h"
  27. #include "fsl_ssi.h"
  28. #include "fsl_utils.h"
  29. /* P1022-specific PMUXCR and DMUXCR bit definitions */
  30. #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
  31. #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
  32. #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
  33. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
  34. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
  35. #define CCSR_GUTS_DMUXCR_PAD 1 /* DMA controller/channel set to pad */
  36. #define CCSR_GUTS_DMUXCR_SSI 2 /* DMA controller/channel set to SSI */
  37. /*
  38. * Set the DMACR register in the GUTS
  39. *
  40. * The DMACR register determines the source of initiated transfers for each
  41. * channel on each DMA controller. Rather than have a bunch of repetitive
  42. * macros for the bit patterns, we just have a function that calculates
  43. * them.
  44. *
  45. * guts: Pointer to GUTS structure
  46. * co: The DMA controller (0 or 1)
  47. * ch: The channel on the DMA controller (0, 1, 2, or 3)
  48. * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
  49. */
  50. static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
  51. unsigned int co, unsigned int ch, unsigned int device)
  52. {
  53. unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
  54. clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
  55. }
  56. /* There's only one global utilities register */
  57. static phys_addr_t guts_phys;
  58. /**
  59. * machine_data: machine-specific ASoC device data
  60. *
  61. * This structure contains data for a single sound platform device on an
  62. * P1022 RDK. Some of the data is taken from the device tree.
  63. */
  64. struct machine_data {
  65. struct snd_soc_dai_link dai[2];
  66. struct snd_soc_card card;
  67. unsigned int dai_format;
  68. unsigned int codec_clk_direction;
  69. unsigned int cpu_clk_direction;
  70. unsigned int clk_frequency;
  71. unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
  72. unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
  73. char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
  74. };
  75. /**
  76. * p1022_rdk_machine_probe: initialize the board
  77. *
  78. * This function is used to initialize the board-specific hardware.
  79. *
  80. * Here we program the DMACR and PMUXCR registers.
  81. */
  82. static int p1022_rdk_machine_probe(struct snd_soc_card *card)
  83. {
  84. struct machine_data *mdata =
  85. container_of(card, struct machine_data, card);
  86. struct ccsr_guts __iomem *guts;
  87. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  88. if (!guts) {
  89. dev_err(card->dev, "could not map global utilities\n");
  90. return -ENOMEM;
  91. }
  92. /* Enable SSI Tx signal */
  93. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
  94. CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
  95. /* Enable SSI Rx signal */
  96. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
  97. CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
  98. /* Enable DMA Channel for SSI */
  99. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
  100. CCSR_GUTS_DMUXCR_SSI);
  101. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
  102. CCSR_GUTS_DMUXCR_SSI);
  103. iounmap(guts);
  104. return 0;
  105. }
  106. /**
  107. * p1022_rdk_startup: program the board with various hardware parameters
  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. static int p1022_rdk_startup(struct snd_pcm_substream *substream)
  114. {
  115. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  116. struct machine_data *mdata =
  117. container_of(rtd->card, struct machine_data, card);
  118. struct device *dev = rtd->card->dev;
  119. int ret = 0;
  120. /* Tell the codec driver what the serial protocol is. */
  121. ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
  122. if (ret < 0) {
  123. dev_err(dev, "could not set codec driver audio format (ret=%i)\n",
  124. ret);
  125. return ret;
  126. }
  127. ret = snd_soc_dai_set_pll(rtd->codec_dai, 0, 0, mdata->clk_frequency,
  128. mdata->clk_frequency);
  129. if (ret < 0) {
  130. dev_err(dev, "could not set codec PLL frequency (ret=%i)\n",
  131. ret);
  132. return ret;
  133. }
  134. return 0;
  135. }
  136. /**
  137. * p1022_rdk_machine_remove: Remove the sound device
  138. *
  139. * This function is called to remove the sound device for one SSI. We
  140. * de-program the DMACR and PMUXCR register.
  141. */
  142. static int p1022_rdk_machine_remove(struct snd_soc_card *card)
  143. {
  144. struct machine_data *mdata =
  145. container_of(card, struct machine_data, card);
  146. struct ccsr_guts __iomem *guts;
  147. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  148. if (!guts) {
  149. dev_err(card->dev, "could not map global utilities\n");
  150. return -ENOMEM;
  151. }
  152. /* Restore the signal routing */
  153. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
  154. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
  155. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
  156. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
  157. iounmap(guts);
  158. return 0;
  159. }
  160. /**
  161. * p1022_rdk_ops: ASoC machine driver operations
  162. */
  163. static struct snd_soc_ops p1022_rdk_ops = {
  164. .startup = p1022_rdk_startup,
  165. };
  166. /**
  167. * p1022_rdk_probe: platform probe function for the machine driver
  168. *
  169. * Although this is a machine driver, the SSI node is the "master" node with
  170. * respect to audio hardware connections. Therefore, we create a new ASoC
  171. * device for each new SSI node that has a codec attached.
  172. */
  173. static int p1022_rdk_probe(struct platform_device *pdev)
  174. {
  175. struct device *dev = pdev->dev.parent;
  176. /* ssi_pdev is the platform device for the SSI node that probed us */
  177. struct platform_device *ssi_pdev = to_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. },
  302. };
  303. /**
  304. * p1022_rdk_init: machine driver initialization.
  305. *
  306. * This function is called when this module is loaded.
  307. */
  308. static int __init p1022_rdk_init(void)
  309. {
  310. struct device_node *guts_np;
  311. struct resource res;
  312. /* Get the physical address of the global utilities registers */
  313. guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
  314. if (of_address_to_resource(guts_np, 0, &res)) {
  315. pr_err("snd-soc-p1022rdk: missing/invalid global utils node\n");
  316. of_node_put(guts_np);
  317. return -EINVAL;
  318. }
  319. guts_phys = res.start;
  320. of_node_put(guts_np);
  321. return platform_driver_register(&p1022_rdk_driver);
  322. }
  323. /**
  324. * p1022_rdk_exit: machine driver exit
  325. *
  326. * This function is called when this driver is unloaded.
  327. */
  328. static void __exit p1022_rdk_exit(void)
  329. {
  330. platform_driver_unregister(&p1022_rdk_driver);
  331. }
  332. late_initcall(p1022_rdk_init);
  333. module_exit(p1022_rdk_exit);
  334. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  335. MODULE_DESCRIPTION("Freescale / iVeia P1022 RDK ALSA SoC machine driver");
  336. MODULE_LICENSE("GPL v2");