p1022_ds.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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/interrupt.h>
  14. #include <linux/of_device.h>
  15. #include <linux/slab.h>
  16. #include <sound/soc.h>
  17. #include <asm/fsl_guts.h>
  18. #include "fsl_dma.h"
  19. #include "fsl_ssi.h"
  20. #include "fsl_utils.h"
  21. /* P1022-specific PMUXCR and DMUXCR bit definitions */
  22. #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
  23. #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
  24. #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
  25. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
  26. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
  27. #define CCSR_GUTS_DMUXCR_PAD 1 /* DMA controller/channel set to pad */
  28. #define CCSR_GUTS_DMUXCR_SSI 2 /* DMA controller/channel set to SSI */
  29. /*
  30. * Set the DMACR register in the GUTS
  31. *
  32. * The DMACR register determines the source of initiated transfers for each
  33. * channel on each DMA controller. Rather than have a bunch of repetitive
  34. * macros for the bit patterns, we just have a function that calculates
  35. * them.
  36. *
  37. * guts: Pointer to GUTS structure
  38. * co: The DMA controller (0 or 1)
  39. * ch: The channel on the DMA controller (0, 1, 2, or 3)
  40. * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
  41. */
  42. static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
  43. unsigned int co, unsigned int ch, unsigned int device)
  44. {
  45. unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
  46. clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
  47. }
  48. /* There's only one global utilities register */
  49. static phys_addr_t guts_phys;
  50. /**
  51. * machine_data: machine-specific ASoC device data
  52. *
  53. * This structure contains data for a single sound platform device on an
  54. * P1022 DS. Some of the data is taken from the device tree.
  55. */
  56. struct machine_data {
  57. struct snd_soc_dai_link dai[2];
  58. struct snd_soc_card card;
  59. unsigned int dai_format;
  60. unsigned int codec_clk_direction;
  61. unsigned int cpu_clk_direction;
  62. unsigned int clk_frequency;
  63. unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
  64. unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
  65. unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
  66. char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
  67. };
  68. /**
  69. * p1022_ds_machine_probe: initialize the board
  70. *
  71. * This function is used to initialize the board-specific hardware.
  72. *
  73. * Here we program the DMACR and PMUXCR registers.
  74. */
  75. static int p1022_ds_machine_probe(struct snd_soc_card *card)
  76. {
  77. struct machine_data *mdata =
  78. container_of(card, struct machine_data, card);
  79. struct ccsr_guts __iomem *guts;
  80. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  81. if (!guts) {
  82. dev_err(card->dev, "could not map global utilities\n");
  83. return -ENOMEM;
  84. }
  85. /* Enable SSI Tx signal */
  86. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
  87. CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
  88. /* Enable SSI Rx signal */
  89. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
  90. CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
  91. /* Enable DMA Channel for SSI */
  92. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
  93. CCSR_GUTS_DMUXCR_SSI);
  94. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
  95. CCSR_GUTS_DMUXCR_SSI);
  96. iounmap(guts);
  97. return 0;
  98. }
  99. /**
  100. * p1022_ds_startup: program the board with various hardware parameters
  101. *
  102. * This function takes board-specific information, like clock frequencies
  103. * and serial data formats, and passes that information to the codec and
  104. * transport drivers.
  105. */
  106. static int p1022_ds_startup(struct snd_pcm_substream *substream)
  107. {
  108. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109. struct machine_data *mdata =
  110. container_of(rtd->card, struct machine_data, card);
  111. struct device *dev = rtd->card->dev;
  112. int ret = 0;
  113. /* Tell the codec driver what the serial protocol is. */
  114. ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
  115. if (ret < 0) {
  116. dev_err(dev, "could not set codec driver audio format\n");
  117. return ret;
  118. }
  119. /*
  120. * Tell the codec driver what the MCLK frequency is, and whether it's
  121. * a slave or master.
  122. */
  123. ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0, mdata->clk_frequency,
  124. mdata->codec_clk_direction);
  125. if (ret < 0) {
  126. dev_err(dev, "could not set codec driver clock params\n");
  127. return ret;
  128. }
  129. return 0;
  130. }
  131. /**
  132. * p1022_ds_machine_remove: Remove the sound device
  133. *
  134. * This function is called to remove the sound device for one SSI. We
  135. * de-program the DMACR and PMUXCR register.
  136. */
  137. static int p1022_ds_machine_remove(struct snd_soc_card *card)
  138. {
  139. struct machine_data *mdata =
  140. container_of(card, struct machine_data, card);
  141. struct ccsr_guts __iomem *guts;
  142. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  143. if (!guts) {
  144. dev_err(card->dev, "could not map global utilities\n");
  145. return -ENOMEM;
  146. }
  147. /* Restore the signal routing */
  148. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
  149. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
  150. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
  151. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
  152. iounmap(guts);
  153. return 0;
  154. }
  155. /**
  156. * p1022_ds_ops: ASoC machine driver operations
  157. */
  158. static struct snd_soc_ops p1022_ds_ops = {
  159. .startup = p1022_ds_startup,
  160. };
  161. /**
  162. * p1022_ds_probe: platform probe function for the machine driver
  163. *
  164. * Although this is a machine driver, the SSI node is the "master" node with
  165. * respect to audio hardware connections. Therefore, we create a new ASoC
  166. * device for each new SSI node that has a codec attached.
  167. */
  168. static int p1022_ds_probe(struct platform_device *pdev)
  169. {
  170. struct device *dev = pdev->dev.parent;
  171. /* ssi_pdev is the platform device for the SSI node that probed us */
  172. struct platform_device *ssi_pdev =
  173. container_of(dev, struct platform_device, dev);
  174. struct device_node *np = ssi_pdev->dev.of_node;
  175. struct device_node *codec_np = NULL;
  176. struct platform_device *sound_device = 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.num_links = 2;
  309. mdata->card.dai_link = mdata->dai;
  310. /* Allocate a new audio platform device structure */
  311. sound_device = platform_device_alloc("soc-audio", -1);
  312. if (!sound_device) {
  313. dev_err(&pdev->dev, "platform device alloc failed\n");
  314. ret = -ENOMEM;
  315. goto error;
  316. }
  317. /* Associate the card data with the sound device */
  318. platform_set_drvdata(sound_device, &mdata->card);
  319. /* Register with ASoC */
  320. ret = platform_device_add(sound_device);
  321. if (ret) {
  322. dev_err(&pdev->dev, "platform device add failed\n");
  323. goto error;
  324. }
  325. dev_set_drvdata(&pdev->dev, sound_device);
  326. of_node_put(codec_np);
  327. return 0;
  328. error:
  329. if (sound_device)
  330. platform_device_put(sound_device);
  331. kfree(mdata);
  332. error_put:
  333. of_node_put(codec_np);
  334. return ret;
  335. }
  336. /**
  337. * p1022_ds_remove: remove the platform device
  338. *
  339. * This function is called when the platform device is removed.
  340. */
  341. static int __devexit p1022_ds_remove(struct platform_device *pdev)
  342. {
  343. struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
  344. struct snd_soc_card *card = platform_get_drvdata(sound_device);
  345. struct machine_data *mdata =
  346. container_of(card, struct machine_data, card);
  347. platform_device_unregister(sound_device);
  348. kfree(mdata);
  349. sound_device->dev.platform_data = NULL;
  350. dev_set_drvdata(&pdev->dev, NULL);
  351. return 0;
  352. }
  353. static struct platform_driver p1022_ds_driver = {
  354. .probe = p1022_ds_probe,
  355. .remove = __devexit_p(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. .owner = THIS_MODULE,
  363. },
  364. };
  365. /**
  366. * p1022_ds_init: machine driver initialization.
  367. *
  368. * This function is called when this module is loaded.
  369. */
  370. static int __init p1022_ds_init(void)
  371. {
  372. struct device_node *guts_np;
  373. struct resource res;
  374. /* Get the physical address of the global utilities registers */
  375. guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
  376. if (of_address_to_resource(guts_np, 0, &res)) {
  377. pr_err("snd-soc-p1022ds: missing/invalid global utils node\n");
  378. of_node_put(guts_np);
  379. return -EINVAL;
  380. }
  381. guts_phys = res.start;
  382. of_node_put(guts_np);
  383. return platform_driver_register(&p1022_ds_driver);
  384. }
  385. /**
  386. * p1022_ds_exit: machine driver exit
  387. *
  388. * This function is called when this driver is unloaded.
  389. */
  390. static void __exit p1022_ds_exit(void)
  391. {
  392. platform_driver_unregister(&p1022_ds_driver);
  393. }
  394. module_init(p1022_ds_init);
  395. module_exit(p1022_ds_exit);
  396. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  397. MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
  398. MODULE_LICENSE("GPL v2");