mpc8610_hpcd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**
  2. * Freescale MPC8610HPCD ALSA SoC Machine driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2007-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/fsl/guts.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. /* There's only one global utilities register */
  23. static phys_addr_t guts_phys;
  24. /**
  25. * mpc8610_hpcd_data: machine-specific ASoC device data
  26. *
  27. * This structure contains data for a single sound platform device on an
  28. * MPC8610 HPCD. Some of the data is taken from the device tree.
  29. */
  30. struct mpc8610_hpcd_data {
  31. struct snd_soc_dai_link dai[2];
  32. struct snd_soc_card card;
  33. unsigned int dai_format;
  34. unsigned int codec_clk_direction;
  35. unsigned int cpu_clk_direction;
  36. unsigned int clk_frequency;
  37. unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
  38. unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
  39. unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
  40. char codec_dai_name[DAI_NAME_SIZE];
  41. char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
  42. };
  43. /**
  44. * mpc8610_hpcd_machine_probe: initialize the board
  45. *
  46. * This function is used to initialize the board-specific hardware.
  47. *
  48. * Here we program the DMACR and PMUXCR registers.
  49. */
  50. static int mpc8610_hpcd_machine_probe(struct snd_soc_card *card)
  51. {
  52. struct mpc8610_hpcd_data *machine_data =
  53. container_of(card, struct mpc8610_hpcd_data, card);
  54. struct ccsr_guts __iomem *guts;
  55. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  56. if (!guts) {
  57. dev_err(card->dev, "could not map global utilities\n");
  58. return -ENOMEM;
  59. }
  60. /* Program the signal routing between the SSI and the DMA */
  61. guts_set_dmacr(guts, machine_data->dma_id[0],
  62. machine_data->dma_channel_id[0],
  63. CCSR_GUTS_DMACR_DEV_SSI);
  64. guts_set_dmacr(guts, machine_data->dma_id[1],
  65. machine_data->dma_channel_id[1],
  66. CCSR_GUTS_DMACR_DEV_SSI);
  67. guts_set_pmuxcr_dma(guts, machine_data->dma_id[0],
  68. machine_data->dma_channel_id[0], 0);
  69. guts_set_pmuxcr_dma(guts, machine_data->dma_id[1],
  70. machine_data->dma_channel_id[1], 0);
  71. switch (machine_data->ssi_id) {
  72. case 0:
  73. clrsetbits_be32(&guts->pmuxcr,
  74. CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
  75. break;
  76. case 1:
  77. clrsetbits_be32(&guts->pmuxcr,
  78. CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
  79. break;
  80. }
  81. iounmap(guts);
  82. return 0;
  83. }
  84. /**
  85. * mpc8610_hpcd_startup: program the board with various hardware parameters
  86. *
  87. * This function takes board-specific information, like clock frequencies
  88. * and serial data formats, and passes that information to the codec and
  89. * transport drivers.
  90. */
  91. static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
  92. {
  93. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  94. struct mpc8610_hpcd_data *machine_data =
  95. container_of(rtd->card, struct mpc8610_hpcd_data, card);
  96. struct device *dev = rtd->card->dev;
  97. int ret = 0;
  98. /* Tell the codec driver what the serial protocol is. */
  99. ret = snd_soc_dai_set_fmt(rtd->codec_dai, machine_data->dai_format);
  100. if (ret < 0) {
  101. dev_err(dev, "could not set codec driver audio format\n");
  102. return ret;
  103. }
  104. /*
  105. * Tell the codec driver what the MCLK frequency is, and whether it's
  106. * a slave or master.
  107. */
  108. ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0,
  109. machine_data->clk_frequency,
  110. machine_data->codec_clk_direction);
  111. if (ret < 0) {
  112. dev_err(dev, "could not set codec driver clock params\n");
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. /**
  118. * mpc8610_hpcd_machine_remove: Remove the sound device
  119. *
  120. * This function is called to remove the sound device for one SSI. We
  121. * de-program the DMACR and PMUXCR register.
  122. */
  123. static int mpc8610_hpcd_machine_remove(struct snd_soc_card *card)
  124. {
  125. struct mpc8610_hpcd_data *machine_data =
  126. container_of(card, struct mpc8610_hpcd_data, card);
  127. struct ccsr_guts __iomem *guts;
  128. guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
  129. if (!guts) {
  130. dev_err(card->dev, "could not map global utilities\n");
  131. return -ENOMEM;
  132. }
  133. /* Restore the signal routing */
  134. guts_set_dmacr(guts, machine_data->dma_id[0],
  135. machine_data->dma_channel_id[0], 0);
  136. guts_set_dmacr(guts, machine_data->dma_id[1],
  137. machine_data->dma_channel_id[1], 0);
  138. switch (machine_data->ssi_id) {
  139. case 0:
  140. clrsetbits_be32(&guts->pmuxcr,
  141. CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
  142. break;
  143. case 1:
  144. clrsetbits_be32(&guts->pmuxcr,
  145. CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_LA);
  146. break;
  147. }
  148. iounmap(guts);
  149. return 0;
  150. }
  151. /**
  152. * mpc8610_hpcd_ops: ASoC machine driver operations
  153. */
  154. static struct snd_soc_ops mpc8610_hpcd_ops = {
  155. .startup = mpc8610_hpcd_startup,
  156. };
  157. /**
  158. * mpc8610_hpcd_probe: platform probe function for the machine driver
  159. *
  160. * Although this is a machine driver, the SSI node is the "master" node with
  161. * respect to audio hardware connections. Therefore, we create a new ASoC
  162. * device for each new SSI node that has a codec attached.
  163. */
  164. static int mpc8610_hpcd_probe(struct platform_device *pdev)
  165. {
  166. struct device *dev = pdev->dev.parent;
  167. /* ssi_pdev is the platform device for the SSI node that probed us */
  168. struct platform_device *ssi_pdev =
  169. container_of(dev, struct platform_device, dev);
  170. struct device_node *np = ssi_pdev->dev.of_node;
  171. struct device_node *codec_np = NULL;
  172. struct mpc8610_hpcd_data *machine_data;
  173. int ret = -ENODEV;
  174. const char *sprop;
  175. const u32 *iprop;
  176. /* Find the codec node for this SSI. */
  177. codec_np = of_parse_phandle(np, "codec-handle", 0);
  178. if (!codec_np) {
  179. dev_err(dev, "invalid codec node\n");
  180. return -EINVAL;
  181. }
  182. machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
  183. if (!machine_data) {
  184. ret = -ENOMEM;
  185. goto error_alloc;
  186. }
  187. machine_data->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
  188. machine_data->dai[0].ops = &mpc8610_hpcd_ops;
  189. /* ASoC core can match codec with device node */
  190. machine_data->dai[0].codec_of_node = codec_np;
  191. /* The DAI name from the codec (snd_soc_dai_driver.name) */
  192. machine_data->dai[0].codec_dai_name = "cs4270-hifi";
  193. /* We register two DAIs per SSI, one for playback and the other for
  194. * capture. Currently, we only support codecs that have one DAI for
  195. * both playback and capture.
  196. */
  197. memcpy(&machine_data->dai[1], &machine_data->dai[0],
  198. sizeof(struct snd_soc_dai_link));
  199. /* Get the device ID */
  200. iprop = of_get_property(np, "cell-index", NULL);
  201. if (!iprop) {
  202. dev_err(&pdev->dev, "cell-index property not found\n");
  203. ret = -EINVAL;
  204. goto error;
  205. }
  206. machine_data->ssi_id = be32_to_cpup(iprop);
  207. /* Get the serial format and clock direction. */
  208. sprop = of_get_property(np, "fsl,mode", NULL);
  209. if (!sprop) {
  210. dev_err(&pdev->dev, "fsl,mode property not found\n");
  211. ret = -EINVAL;
  212. goto error;
  213. }
  214. if (strcasecmp(sprop, "i2s-slave") == 0) {
  215. machine_data->dai_format =
  216. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
  217. machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
  218. machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
  219. /* In i2s-slave mode, the codec has its own clock source, so we
  220. * need to get the frequency from the device tree and pass it to
  221. * the codec driver.
  222. */
  223. iprop = of_get_property(codec_np, "clock-frequency", NULL);
  224. if (!iprop || !*iprop) {
  225. dev_err(&pdev->dev, "codec bus-frequency "
  226. "property is missing or invalid\n");
  227. ret = -EINVAL;
  228. goto error;
  229. }
  230. machine_data->clk_frequency = be32_to_cpup(iprop);
  231. } else if (strcasecmp(sprop, "i2s-master") == 0) {
  232. machine_data->dai_format =
  233. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
  234. machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
  235. machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  236. } else if (strcasecmp(sprop, "lj-slave") == 0) {
  237. machine_data->dai_format =
  238. SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
  239. machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
  240. machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
  241. } else if (strcasecmp(sprop, "lj-master") == 0) {
  242. machine_data->dai_format =
  243. SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
  244. machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
  245. machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  246. } else if (strcasecmp(sprop, "rj-slave") == 0) {
  247. machine_data->dai_format =
  248. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
  249. machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
  250. machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
  251. } else if (strcasecmp(sprop, "rj-master") == 0) {
  252. machine_data->dai_format =
  253. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
  254. machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
  255. machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  256. } else if (strcasecmp(sprop, "ac97-slave") == 0) {
  257. machine_data->dai_format =
  258. SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
  259. machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
  260. machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
  261. } else if (strcasecmp(sprop, "ac97-master") == 0) {
  262. machine_data->dai_format =
  263. SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
  264. machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
  265. machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  266. } else {
  267. dev_err(&pdev->dev,
  268. "unrecognized fsl,mode property '%s'\n", sprop);
  269. ret = -EINVAL;
  270. goto error;
  271. }
  272. if (!machine_data->clk_frequency) {
  273. dev_err(&pdev->dev, "unknown clock frequency\n");
  274. ret = -EINVAL;
  275. goto error;
  276. }
  277. /* Find the playback DMA channel to use. */
  278. machine_data->dai[0].platform_name = machine_data->platform_name[0];
  279. ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma",
  280. &machine_data->dai[0],
  281. &machine_data->dma_channel_id[0],
  282. &machine_data->dma_id[0]);
  283. if (ret) {
  284. dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
  285. goto error;
  286. }
  287. /* Find the capture DMA channel to use. */
  288. machine_data->dai[1].platform_name = machine_data->platform_name[1];
  289. ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma",
  290. &machine_data->dai[1],
  291. &machine_data->dma_channel_id[1],
  292. &machine_data->dma_id[1]);
  293. if (ret) {
  294. dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
  295. goto error;
  296. }
  297. /* Initialize our DAI data structure. */
  298. machine_data->dai[0].stream_name = "playback";
  299. machine_data->dai[1].stream_name = "capture";
  300. machine_data->dai[0].name = machine_data->dai[0].stream_name;
  301. machine_data->dai[1].name = machine_data->dai[1].stream_name;
  302. machine_data->card.probe = mpc8610_hpcd_machine_probe;
  303. machine_data->card.remove = mpc8610_hpcd_machine_remove;
  304. machine_data->card.name = pdev->name; /* The platform driver name */
  305. machine_data->card.owner = THIS_MODULE;
  306. machine_data->card.dev = &pdev->dev;
  307. machine_data->card.num_links = 2;
  308. machine_data->card.dai_link = machine_data->dai;
  309. /* Register with ASoC */
  310. ret = snd_soc_register_card(&machine_data->card);
  311. if (ret) {
  312. dev_err(&pdev->dev, "could not register card\n");
  313. goto error;
  314. }
  315. of_node_put(codec_np);
  316. return 0;
  317. error:
  318. kfree(machine_data);
  319. error_alloc:
  320. of_node_put(codec_np);
  321. return ret;
  322. }
  323. /**
  324. * mpc8610_hpcd_remove: remove the platform device
  325. *
  326. * This function is called when the platform device is removed.
  327. */
  328. static int mpc8610_hpcd_remove(struct platform_device *pdev)
  329. {
  330. struct snd_soc_card *card = platform_get_drvdata(pdev);
  331. struct mpc8610_hpcd_data *machine_data =
  332. container_of(card, struct mpc8610_hpcd_data, card);
  333. snd_soc_unregister_card(card);
  334. kfree(machine_data);
  335. return 0;
  336. }
  337. static struct platform_driver mpc8610_hpcd_driver = {
  338. .probe = mpc8610_hpcd_probe,
  339. .remove = mpc8610_hpcd_remove,
  340. .driver = {
  341. /* The name must match 'compatible' property in the device tree,
  342. * in lowercase letters.
  343. */
  344. .name = "snd-soc-mpc8610hpcd",
  345. },
  346. };
  347. /**
  348. * mpc8610_hpcd_init: machine driver initialization.
  349. *
  350. * This function is called when this module is loaded.
  351. */
  352. static int __init mpc8610_hpcd_init(void)
  353. {
  354. struct device_node *guts_np;
  355. struct resource res;
  356. pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
  357. /* Get the physical address of the global utilities registers */
  358. guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
  359. if (of_address_to_resource(guts_np, 0, &res)) {
  360. pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
  361. return -EINVAL;
  362. }
  363. guts_phys = res.start;
  364. return platform_driver_register(&mpc8610_hpcd_driver);
  365. }
  366. /**
  367. * mpc8610_hpcd_exit: machine driver exit
  368. *
  369. * This function is called when this driver is unloaded.
  370. */
  371. static void __exit mpc8610_hpcd_exit(void)
  372. {
  373. platform_driver_unregister(&mpc8610_hpcd_driver);
  374. }
  375. module_init(mpc8610_hpcd_init);
  376. module_exit(mpc8610_hpcd_exit);
  377. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  378. MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
  379. MODULE_LICENSE("GPL v2");