p1022_ds.c 14 KB

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