p1022_ds.c 13 KB

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