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 =
  178. container_of(dev, struct platform_device, dev);
  179. struct device_node *np = ssi_pdev->dev.of_node;
  180. struct device_node *codec_np = NULL;
  181. struct machine_data *mdata;
  182. const u32 *iprop;
  183. int ret;
  184. /* Find the codec node for this SSI. */
  185. codec_np = of_parse_phandle(np, "codec-handle", 0);
  186. if (!codec_np) {
  187. dev_err(dev, "could not find codec node\n");
  188. return -EINVAL;
  189. }
  190. mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
  191. if (!mdata) {
  192. ret = -ENOMEM;
  193. goto error_put;
  194. }
  195. mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
  196. mdata->dai[0].ops = &p1022_rdk_ops;
  197. /* ASoC core can match codec with device node */
  198. mdata->dai[0].codec_of_node = codec_np;
  199. /*
  200. * We register two DAIs per SSI, one for playback and the other for
  201. * capture. We support codecs that have separate DAIs for both playback
  202. * and capture.
  203. */
  204. memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
  205. /* The DAI names from the codec (snd_soc_dai_driver.name) */
  206. mdata->dai[0].codec_dai_name = "wm8960-hifi";
  207. mdata->dai[1].codec_dai_name = mdata->dai[0].codec_dai_name;
  208. /*
  209. * Configure the SSI for I2S slave mode. Older device trees have
  210. * an fsl,mode property, but we ignore that since there's really
  211. * only one way to configure the SSI.
  212. */
  213. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  214. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
  215. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  216. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  217. /*
  218. * In i2s-slave mode, the codec has its own clock source, so we
  219. * need to get the frequency from the device tree and pass it to
  220. * the codec driver.
  221. */
  222. iprop = of_get_property(codec_np, "clock-frequency", NULL);
  223. if (!iprop || !*iprop) {
  224. dev_err(&pdev->dev, "codec bus-frequency property is missing or invalid\n");
  225. ret = -EINVAL;
  226. goto error;
  227. }
  228. mdata->clk_frequency = be32_to_cpup(iprop);
  229. if (!mdata->clk_frequency) {
  230. dev_err(&pdev->dev, "unknown clock frequency\n");
  231. ret = -EINVAL;
  232. goto error;
  233. }
  234. /* Find the playback DMA channel to use. */
  235. mdata->dai[0].platform_name = mdata->platform_name[0];
  236. ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
  237. &mdata->dma_channel_id[0],
  238. &mdata->dma_id[0]);
  239. if (ret) {
  240. dev_err(&pdev->dev, "missing/invalid playback DMA phandle (ret=%i)\n",
  241. ret);
  242. goto error;
  243. }
  244. /* Find the capture DMA channel to use. */
  245. mdata->dai[1].platform_name = mdata->platform_name[1];
  246. ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
  247. &mdata->dma_channel_id[1],
  248. &mdata->dma_id[1]);
  249. if (ret) {
  250. dev_err(&pdev->dev, "missing/invalid capture DMA phandle (ret=%i)\n",
  251. ret);
  252. goto error;
  253. }
  254. /* Initialize our DAI data structure. */
  255. mdata->dai[0].stream_name = "playback";
  256. mdata->dai[1].stream_name = "capture";
  257. mdata->dai[0].name = mdata->dai[0].stream_name;
  258. mdata->dai[1].name = mdata->dai[1].stream_name;
  259. mdata->card.probe = p1022_rdk_machine_probe;
  260. mdata->card.remove = p1022_rdk_machine_remove;
  261. mdata->card.name = pdev->name; /* The platform driver name */
  262. mdata->card.owner = THIS_MODULE;
  263. mdata->card.dev = &pdev->dev;
  264. mdata->card.num_links = 2;
  265. mdata->card.dai_link = mdata->dai;
  266. /* Register with ASoC */
  267. ret = snd_soc_register_card(&mdata->card);
  268. if (ret) {
  269. dev_err(&pdev->dev, "could not register card (ret=%i)\n", ret);
  270. goto error;
  271. }
  272. return 0;
  273. error:
  274. kfree(mdata);
  275. error_put:
  276. of_node_put(codec_np);
  277. return ret;
  278. }
  279. /**
  280. * p1022_rdk_remove: remove the platform device
  281. *
  282. * This function is called when the platform device is removed.
  283. */
  284. static int p1022_rdk_remove(struct platform_device *pdev)
  285. {
  286. struct snd_soc_card *card = platform_get_drvdata(pdev);
  287. struct machine_data *mdata =
  288. container_of(card, struct machine_data, card);
  289. snd_soc_unregister_card(card);
  290. kfree(mdata);
  291. return 0;
  292. }
  293. static struct platform_driver p1022_rdk_driver = {
  294. .probe = p1022_rdk_probe,
  295. .remove = p1022_rdk_remove,
  296. .driver = {
  297. /*
  298. * The name must match 'compatible' property in the device tree,
  299. * in lowercase letters.
  300. */
  301. .name = "snd-soc-p1022rdk",
  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");