p1022_ds.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * Freescale P1022DS ALSA SoC Machine driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2010 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. #include <linux/module.h>
  13. #include <linux/fsl/guts.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/slab.h>
  18. #include <sound/soc.h>
  19. #include "fsl_dma.h"
  20. #include "fsl_ssi.h"
  21. #include "fsl_utils.h"
  22. /* P1022-specific PMUXCR and DMUXCR bit definitions */
  23. #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
  24. #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
  25. #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
  26. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
  27. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
  28. #define CCSR_GUTS_DMUXCR_PAD 1 /* DMA controller/channel set to pad */
  29. #define CCSR_GUTS_DMUXCR_SSI 2 /* DMA controller/channel set to SSI */
  30. /*
  31. * Set the DMACR register in the GUTS
  32. *
  33. * The DMACR register determines the source of initiated transfers for each
  34. * channel on each DMA controller. Rather than have a bunch of repetitive
  35. * macros for the bit patterns, we just have a function that calculates
  36. * them.
  37. *
  38. * guts: Pointer to GUTS structure
  39. * co: The DMA controller (0 or 1)
  40. * ch: The channel on the DMA controller (0, 1, 2, or 3)
  41. * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
  42. */
  43. static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
  44. unsigned int co, unsigned int ch, unsigned int device)
  45. {
  46. unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
  47. clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
  48. }
  49. /* There's only one global utilities register */
  50. static phys_addr_t guts_phys;
  51. /**
  52. * machine_data: machine-specific ASoC device data
  53. *
  54. * This structure contains data for a single sound platform device on an
  55. * P1022 DS. Some of the data is taken from the device tree.
  56. */
  57. struct machine_data {
  58. struct snd_soc_dai_link dai[2];
  59. struct snd_soc_card card;
  60. unsigned int dai_format;
  61. unsigned int codec_clk_direction;
  62. unsigned int cpu_clk_direction;
  63. unsigned int clk_frequency;
  64. unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
  65. unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
  66. unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
  67. char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
  68. };
  69. /**
  70. * p1022_ds_machine_probe: initialize the board
  71. *
  72. * This function is used to initialize the board-specific hardware.
  73. *
  74. * Here we program the DMACR and PMUXCR registers.
  75. */
  76. static int p1022_ds_machine_probe(struct snd_soc_card *card)
  77. {
  78. struct machine_data *mdata =
  79. container_of(card, struct machine_data, card);
  80. struct ccsr_guts __iomem *guts;
  81. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  82. if (!guts) {
  83. dev_err(card->dev, "could not map global utilities\n");
  84. return -ENOMEM;
  85. }
  86. /* Enable SSI Tx signal */
  87. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
  88. CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
  89. /* Enable SSI Rx signal */
  90. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
  91. CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
  92. /* Enable DMA Channel for SSI */
  93. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
  94. CCSR_GUTS_DMUXCR_SSI);
  95. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
  96. CCSR_GUTS_DMUXCR_SSI);
  97. iounmap(guts);
  98. return 0;
  99. }
  100. /**
  101. * p1022_ds_startup: program the board with various hardware parameters
  102. *
  103. * This function takes board-specific information, like clock frequencies
  104. * and serial data formats, and passes that information to the codec and
  105. * transport drivers.
  106. */
  107. static int p1022_ds_startup(struct snd_pcm_substream *substream)
  108. {
  109. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  110. struct machine_data *mdata =
  111. container_of(rtd->card, struct machine_data, card);
  112. struct device *dev = rtd->card->dev;
  113. int ret = 0;
  114. /* Tell the codec driver what the serial protocol is. */
  115. ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
  116. if (ret < 0) {
  117. dev_err(dev, "could not set codec driver audio format\n");
  118. return ret;
  119. }
  120. /*
  121. * Tell the codec driver what the MCLK frequency is, and whether it's
  122. * a slave or master.
  123. */
  124. ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0, mdata->clk_frequency,
  125. mdata->codec_clk_direction);
  126. if (ret < 0) {
  127. dev_err(dev, "could not set codec driver clock params\n");
  128. return ret;
  129. }
  130. return 0;
  131. }
  132. /**
  133. * p1022_ds_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_ds_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_ds_ops: ASoC machine driver operations
  158. */
  159. static struct snd_soc_ops p1022_ds_ops = {
  160. .startup = p1022_ds_startup,
  161. };
  162. /**
  163. * p1022_ds_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_ds_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 =
  174. container_of(dev, struct platform_device, dev);
  175. struct device_node *np = ssi_pdev->dev.of_node;
  176. struct device_node *codec_np = NULL;
  177. struct machine_data *mdata;
  178. int ret = -ENODEV;
  179. const char *sprop;
  180. const u32 *iprop;
  181. /* Find the codec node for this SSI. */
  182. codec_np = of_parse_phandle(np, "codec-handle", 0);
  183. if (!codec_np) {
  184. dev_err(dev, "could not find codec node\n");
  185. return -EINVAL;
  186. }
  187. mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
  188. if (!mdata) {
  189. ret = -ENOMEM;
  190. goto error_put;
  191. }
  192. mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
  193. mdata->dai[0].ops = &p1022_ds_ops;
  194. /* ASoC core can match codec with device node */
  195. mdata->dai[0].codec_of_node = codec_np;
  196. /* We register two DAIs per SSI, one for playback and the other for
  197. * capture. We support codecs that have separate DAIs for both playback
  198. * and capture.
  199. */
  200. memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
  201. /* The DAI names from the codec (snd_soc_dai_driver.name) */
  202. mdata->dai[0].codec_dai_name = "wm8776-hifi-playback";
  203. mdata->dai[1].codec_dai_name = "wm8776-hifi-capture";
  204. /* Get the device ID */
  205. iprop = of_get_property(np, "cell-index", NULL);
  206. if (!iprop) {
  207. dev_err(&pdev->dev, "cell-index property not found\n");
  208. ret = -EINVAL;
  209. goto error;
  210. }
  211. mdata->ssi_id = be32_to_cpup(iprop);
  212. /* Get the serial format and clock direction. */
  213. sprop = of_get_property(np, "fsl,mode", NULL);
  214. if (!sprop) {
  215. dev_err(&pdev->dev, "fsl,mode property not found\n");
  216. ret = -EINVAL;
  217. goto error;
  218. }
  219. if (strcasecmp(sprop, "i2s-slave") == 0) {
  220. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  221. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
  222. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  223. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  224. /* In i2s-slave mode, the codec has its own clock source, so we
  225. * need to get the frequency from the device tree and pass it to
  226. * the codec driver.
  227. */
  228. iprop = of_get_property(codec_np, "clock-frequency", NULL);
  229. if (!iprop || !*iprop) {
  230. dev_err(&pdev->dev, "codec bus-frequency "
  231. "property is missing or invalid\n");
  232. ret = -EINVAL;
  233. goto error;
  234. }
  235. mdata->clk_frequency = be32_to_cpup(iprop);
  236. } else if (strcasecmp(sprop, "i2s-master") == 0) {
  237. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  238. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
  239. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  240. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  241. } else if (strcasecmp(sprop, "lj-slave") == 0) {
  242. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  243. SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
  244. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  245. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  246. } else if (strcasecmp(sprop, "lj-master") == 0) {
  247. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  248. SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
  249. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  250. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  251. } else if (strcasecmp(sprop, "rj-slave") == 0) {
  252. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  253. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
  254. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  255. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  256. } else if (strcasecmp(sprop, "rj-master") == 0) {
  257. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  258. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
  259. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  260. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  261. } else if (strcasecmp(sprop, "ac97-slave") == 0) {
  262. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  263. SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
  264. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  265. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  266. } else if (strcasecmp(sprop, "ac97-master") == 0) {
  267. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  268. SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
  269. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  270. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  271. } else {
  272. dev_err(&pdev->dev,
  273. "unrecognized fsl,mode property '%s'\n", sprop);
  274. ret = -EINVAL;
  275. goto error;
  276. }
  277. if (!mdata->clk_frequency) {
  278. dev_err(&pdev->dev, "unknown clock frequency\n");
  279. ret = -EINVAL;
  280. goto error;
  281. }
  282. /* Find the playback DMA channel to use. */
  283. mdata->dai[0].platform_name = mdata->platform_name[0];
  284. ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
  285. &mdata->dma_channel_id[0],
  286. &mdata->dma_id[0]);
  287. if (ret) {
  288. dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
  289. goto error;
  290. }
  291. /* Find the capture DMA channel to use. */
  292. mdata->dai[1].platform_name = mdata->platform_name[1];
  293. ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
  294. &mdata->dma_channel_id[1],
  295. &mdata->dma_id[1]);
  296. if (ret) {
  297. dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
  298. goto error;
  299. }
  300. /* Initialize our DAI data structure. */
  301. mdata->dai[0].stream_name = "playback";
  302. mdata->dai[1].stream_name = "capture";
  303. mdata->dai[0].name = mdata->dai[0].stream_name;
  304. mdata->dai[1].name = mdata->dai[1].stream_name;
  305. mdata->card.probe = p1022_ds_machine_probe;
  306. mdata->card.remove = p1022_ds_machine_remove;
  307. mdata->card.name = pdev->name; /* The platform driver name */
  308. mdata->card.owner = THIS_MODULE;
  309. mdata->card.dev = &pdev->dev;
  310. mdata->card.num_links = 2;
  311. mdata->card.dai_link = mdata->dai;
  312. /* Register with ASoC */
  313. ret = snd_soc_register_card(&mdata->card);
  314. if (ret) {
  315. dev_err(&pdev->dev, "could not register card\n");
  316. goto error;
  317. }
  318. of_node_put(codec_np);
  319. return 0;
  320. error:
  321. kfree(mdata);
  322. error_put:
  323. of_node_put(codec_np);
  324. return ret;
  325. }
  326. /**
  327. * p1022_ds_remove: remove the platform device
  328. *
  329. * This function is called when the platform device is removed.
  330. */
  331. static int p1022_ds_remove(struct platform_device *pdev)
  332. {
  333. struct snd_soc_card *card = platform_get_drvdata(pdev);
  334. struct machine_data *mdata =
  335. container_of(card, struct machine_data, card);
  336. snd_soc_unregister_card(card);
  337. kfree(mdata);
  338. return 0;
  339. }
  340. static struct platform_driver p1022_ds_driver = {
  341. .probe = p1022_ds_probe,
  342. .remove = p1022_ds_remove,
  343. .driver = {
  344. /*
  345. * The name must match 'compatible' property in the device tree,
  346. * in lowercase letters.
  347. */
  348. .name = "snd-soc-p1022ds",
  349. },
  350. };
  351. /**
  352. * p1022_ds_init: machine driver initialization.
  353. *
  354. * This function is called when this module is loaded.
  355. */
  356. static int __init p1022_ds_init(void)
  357. {
  358. struct device_node *guts_np;
  359. struct resource res;
  360. /* Get the physical address of the global utilities registers */
  361. guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
  362. if (of_address_to_resource(guts_np, 0, &res)) {
  363. pr_err("snd-soc-p1022ds: missing/invalid global utils node\n");
  364. of_node_put(guts_np);
  365. return -EINVAL;
  366. }
  367. guts_phys = res.start;
  368. of_node_put(guts_np);
  369. return platform_driver_register(&p1022_ds_driver);
  370. }
  371. /**
  372. * p1022_ds_exit: machine driver exit
  373. *
  374. * This function is called when this driver is unloaded.
  375. */
  376. static void __exit p1022_ds_exit(void)
  377. {
  378. platform_driver_unregister(&p1022_ds_driver);
  379. }
  380. module_init(p1022_ds_init);
  381. module_exit(p1022_ds_exit);
  382. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  383. MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
  384. MODULE_LICENSE("GPL v2");