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 = to_platform_device(dev);
  174. struct device_node *np = ssi_pdev->dev.of_node;
  175. struct device_node *codec_np = NULL;
  176. struct machine_data *mdata;
  177. int ret = -ENODEV;
  178. const char *sprop;
  179. const u32 *iprop;
  180. /* Find the codec node for this SSI. */
  181. codec_np = of_parse_phandle(np, "codec-handle", 0);
  182. if (!codec_np) {
  183. dev_err(dev, "could not find codec node\n");
  184. return -EINVAL;
  185. }
  186. mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
  187. if (!mdata) {
  188. ret = -ENOMEM;
  189. goto error_put;
  190. }
  191. mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
  192. mdata->dai[0].ops = &p1022_ds_ops;
  193. /* ASoC core can match codec with device node */
  194. mdata->dai[0].codec_of_node = codec_np;
  195. /* We register two DAIs per SSI, one for playback and the other for
  196. * capture. We support codecs that have separate DAIs for both playback
  197. * and capture.
  198. */
  199. memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
  200. /* The DAI names from the codec (snd_soc_dai_driver.name) */
  201. mdata->dai[0].codec_dai_name = "wm8776-hifi-playback";
  202. mdata->dai[1].codec_dai_name = "wm8776-hifi-capture";
  203. /* Get the device ID */
  204. iprop = of_get_property(np, "cell-index", NULL);
  205. if (!iprop) {
  206. dev_err(&pdev->dev, "cell-index property not found\n");
  207. ret = -EINVAL;
  208. goto error;
  209. }
  210. mdata->ssi_id = be32_to_cpup(iprop);
  211. /* Get the serial format and clock direction. */
  212. sprop = of_get_property(np, "fsl,mode", NULL);
  213. if (!sprop) {
  214. dev_err(&pdev->dev, "fsl,mode property not found\n");
  215. ret = -EINVAL;
  216. goto error;
  217. }
  218. if (strcasecmp(sprop, "i2s-slave") == 0) {
  219. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  220. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
  221. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  222. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  223. /* In i2s-slave mode, the codec has its own clock source, so we
  224. * need to get the frequency from the device tree and pass it to
  225. * the codec driver.
  226. */
  227. iprop = of_get_property(codec_np, "clock-frequency", NULL);
  228. if (!iprop || !*iprop) {
  229. dev_err(&pdev->dev, "codec bus-frequency "
  230. "property is missing or invalid\n");
  231. ret = -EINVAL;
  232. goto error;
  233. }
  234. mdata->clk_frequency = be32_to_cpup(iprop);
  235. } else if (strcasecmp(sprop, "i2s-master") == 0) {
  236. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  237. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
  238. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  239. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  240. } else if (strcasecmp(sprop, "lj-slave") == 0) {
  241. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  242. SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
  243. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  244. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  245. } else if (strcasecmp(sprop, "lj-master") == 0) {
  246. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  247. SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
  248. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  249. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  250. } else if (strcasecmp(sprop, "rj-slave") == 0) {
  251. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  252. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
  253. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  254. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  255. } else if (strcasecmp(sprop, "rj-master") == 0) {
  256. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  257. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
  258. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  259. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  260. } else if (strcasecmp(sprop, "ac97-slave") == 0) {
  261. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  262. SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
  263. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  264. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  265. } else if (strcasecmp(sprop, "ac97-master") == 0) {
  266. mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
  267. SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
  268. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  269. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  270. } else {
  271. dev_err(&pdev->dev,
  272. "unrecognized fsl,mode property '%s'\n", sprop);
  273. ret = -EINVAL;
  274. goto error;
  275. }
  276. if (!mdata->clk_frequency) {
  277. dev_err(&pdev->dev, "unknown clock frequency\n");
  278. ret = -EINVAL;
  279. goto error;
  280. }
  281. /* Find the playback DMA channel to use. */
  282. mdata->dai[0].platform_name = mdata->platform_name[0];
  283. ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
  284. &mdata->dma_channel_id[0],
  285. &mdata->dma_id[0]);
  286. if (ret) {
  287. dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
  288. goto error;
  289. }
  290. /* Find the capture DMA channel to use. */
  291. mdata->dai[1].platform_name = mdata->platform_name[1];
  292. ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
  293. &mdata->dma_channel_id[1],
  294. &mdata->dma_id[1]);
  295. if (ret) {
  296. dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
  297. goto error;
  298. }
  299. /* Initialize our DAI data structure. */
  300. mdata->dai[0].stream_name = "playback";
  301. mdata->dai[1].stream_name = "capture";
  302. mdata->dai[0].name = mdata->dai[0].stream_name;
  303. mdata->dai[1].name = mdata->dai[1].stream_name;
  304. mdata->card.probe = p1022_ds_machine_probe;
  305. mdata->card.remove = p1022_ds_machine_remove;
  306. mdata->card.name = pdev->name; /* The platform driver name */
  307. mdata->card.owner = THIS_MODULE;
  308. mdata->card.dev = &pdev->dev;
  309. mdata->card.num_links = 2;
  310. mdata->card.dai_link = mdata->dai;
  311. /* Register with ASoC */
  312. ret = snd_soc_register_card(&mdata->card);
  313. if (ret) {
  314. dev_err(&pdev->dev, "could not register card\n");
  315. goto error;
  316. }
  317. of_node_put(codec_np);
  318. return 0;
  319. error:
  320. kfree(mdata);
  321. error_put:
  322. of_node_put(codec_np);
  323. return ret;
  324. }
  325. /**
  326. * p1022_ds_remove: remove the platform device
  327. *
  328. * This function is called when the platform device is removed.
  329. */
  330. static int p1022_ds_remove(struct platform_device *pdev)
  331. {
  332. struct snd_soc_card *card = platform_get_drvdata(pdev);
  333. struct machine_data *mdata =
  334. container_of(card, struct machine_data, card);
  335. snd_soc_unregister_card(card);
  336. kfree(mdata);
  337. return 0;
  338. }
  339. static struct platform_driver p1022_ds_driver = {
  340. .probe = p1022_ds_probe,
  341. .remove = p1022_ds_remove,
  342. .driver = {
  343. /*
  344. * The name must match 'compatible' property in the device tree,
  345. * in lowercase letters.
  346. */
  347. .name = "snd-soc-p1022ds",
  348. },
  349. };
  350. /**
  351. * p1022_ds_init: machine driver initialization.
  352. *
  353. * This function is called when this module is loaded.
  354. */
  355. static int __init p1022_ds_init(void)
  356. {
  357. struct device_node *guts_np;
  358. struct resource res;
  359. /* Get the physical address of the global utilities registers */
  360. guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
  361. if (of_address_to_resource(guts_np, 0, &res)) {
  362. pr_err("snd-soc-p1022ds: missing/invalid global utils node\n");
  363. of_node_put(guts_np);
  364. return -EINVAL;
  365. }
  366. guts_phys = res.start;
  367. of_node_put(guts_np);
  368. return platform_driver_register(&p1022_ds_driver);
  369. }
  370. /**
  371. * p1022_ds_exit: machine driver exit
  372. *
  373. * This function is called when this driver is unloaded.
  374. */
  375. static void __exit p1022_ds_exit(void)
  376. {
  377. platform_driver_unregister(&p1022_ds_driver);
  378. }
  379. module_init(p1022_ds_init);
  380. module_exit(p1022_ds_exit);
  381. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  382. MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
  383. MODULE_LICENSE("GPL v2");