mpc8610_hpcd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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/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. /* There's only one global utilities register */
  22. static phys_addr_t guts_phys;
  23. /**
  24. * mpc8610_hpcd_data: machine-specific ASoC device data
  25. *
  26. * This structure contains data for a single sound platform device on an
  27. * MPC8610 HPCD. Some of the data is taken from the device tree.
  28. */
  29. struct mpc8610_hpcd_data {
  30. struct snd_soc_dai_link dai[2];
  31. struct snd_soc_card card;
  32. unsigned int dai_format;
  33. unsigned int codec_clk_direction;
  34. unsigned int cpu_clk_direction;
  35. unsigned int clk_frequency;
  36. unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
  37. unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
  38. unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
  39. char codec_dai_name[DAI_NAME_SIZE];
  40. char codec_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_86xx __iomem *guts;
  55. guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
  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_86xx __iomem *guts;
  128. guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
  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 platform_device *sound_device = NULL;
  173. struct mpc8610_hpcd_data *machine_data;
  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, "invalid codec node\n");
  181. return -EINVAL;
  182. }
  183. machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
  184. if (!machine_data) {
  185. ret = -ENOMEM;
  186. goto error_alloc;
  187. }
  188. machine_data->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
  189. machine_data->dai[0].ops = &mpc8610_hpcd_ops;
  190. /* Determine the codec name, it will be used as the codec DAI name */
  191. ret = fsl_asoc_get_codec_dev_name(codec_np, machine_data->codec_name,
  192. DAI_NAME_SIZE);
  193. if (ret) {
  194. dev_err(&pdev->dev, "invalid codec node %s\n",
  195. codec_np->full_name);
  196. ret = -EINVAL;
  197. goto error;
  198. }
  199. machine_data->dai[0].codec_name = machine_data->codec_name;
  200. /* The DAI name from the codec (snd_soc_dai_driver.name) */
  201. machine_data->dai[0].codec_dai_name = "cs4270-hifi";
  202. /* We register two DAIs per SSI, one for playback and the other for
  203. * capture. Currently, we only support codecs that have one DAI for
  204. * both playback and capture.
  205. */
  206. memcpy(&machine_data->dai[1], &machine_data->dai[0],
  207. sizeof(struct snd_soc_dai_link));
  208. /* Get the device ID */
  209. iprop = of_get_property(np, "cell-index", NULL);
  210. if (!iprop) {
  211. dev_err(&pdev->dev, "cell-index property not found\n");
  212. ret = -EINVAL;
  213. goto error;
  214. }
  215. machine_data->ssi_id = be32_to_cpup(iprop);
  216. /* Get the serial format and clock direction. */
  217. sprop = of_get_property(np, "fsl,mode", NULL);
  218. if (!sprop) {
  219. dev_err(&pdev->dev, "fsl,mode property not found\n");
  220. ret = -EINVAL;
  221. goto error;
  222. }
  223. if (strcasecmp(sprop, "i2s-slave") == 0) {
  224. machine_data->dai_format =
  225. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
  226. machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
  227. machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
  228. /* In i2s-slave mode, the codec has its own clock source, so we
  229. * need to get the frequency from the device tree and pass it to
  230. * the codec driver.
  231. */
  232. iprop = of_get_property(codec_np, "clock-frequency", NULL);
  233. if (!iprop || !*iprop) {
  234. dev_err(&pdev->dev, "codec bus-frequency "
  235. "property is missing or invalid\n");
  236. ret = -EINVAL;
  237. goto error;
  238. }
  239. machine_data->clk_frequency = be32_to_cpup(iprop);
  240. } else if (strcasecmp(sprop, "i2s-master") == 0) {
  241. machine_data->dai_format =
  242. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
  243. machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
  244. machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  245. } else if (strcasecmp(sprop, "lj-slave") == 0) {
  246. machine_data->dai_format =
  247. SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
  248. machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
  249. machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
  250. } else if (strcasecmp(sprop, "lj-master") == 0) {
  251. machine_data->dai_format =
  252. SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
  253. machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
  254. machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  255. } else if (strcasecmp(sprop, "rj-slave") == 0) {
  256. machine_data->dai_format =
  257. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
  258. machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
  259. machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
  260. } else if (strcasecmp(sprop, "rj-master") == 0) {
  261. machine_data->dai_format =
  262. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
  263. machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
  264. machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  265. } else if (strcasecmp(sprop, "ac97-slave") == 0) {
  266. machine_data->dai_format =
  267. SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
  268. machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
  269. machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
  270. } else if (strcasecmp(sprop, "ac97-master") == 0) {
  271. machine_data->dai_format =
  272. SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
  273. machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
  274. machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  275. } else {
  276. dev_err(&pdev->dev,
  277. "unrecognized fsl,mode property '%s'\n", sprop);
  278. ret = -EINVAL;
  279. goto error;
  280. }
  281. if (!machine_data->clk_frequency) {
  282. dev_err(&pdev->dev, "unknown clock frequency\n");
  283. ret = -EINVAL;
  284. goto error;
  285. }
  286. /* Find the playback DMA channel to use. */
  287. machine_data->dai[0].platform_name = machine_data->platform_name[0];
  288. ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma",
  289. &machine_data->dai[0],
  290. &machine_data->dma_channel_id[0],
  291. &machine_data->dma_id[0]);
  292. if (ret) {
  293. dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
  294. goto error;
  295. }
  296. /* Find the capture DMA channel to use. */
  297. machine_data->dai[1].platform_name = machine_data->platform_name[1];
  298. ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma",
  299. &machine_data->dai[1],
  300. &machine_data->dma_channel_id[1],
  301. &machine_data->dma_id[1]);
  302. if (ret) {
  303. dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
  304. goto error;
  305. }
  306. /* Initialize our DAI data structure. */
  307. machine_data->dai[0].stream_name = "playback";
  308. machine_data->dai[1].stream_name = "capture";
  309. machine_data->dai[0].name = machine_data->dai[0].stream_name;
  310. machine_data->dai[1].name = machine_data->dai[1].stream_name;
  311. machine_data->card.probe = mpc8610_hpcd_machine_probe;
  312. machine_data->card.remove = mpc8610_hpcd_machine_remove;
  313. machine_data->card.name = pdev->name; /* The platform driver name */
  314. machine_data->card.num_links = 2;
  315. machine_data->card.dai_link = machine_data->dai;
  316. /* Allocate a new audio platform device structure */
  317. sound_device = platform_device_alloc("soc-audio", -1);
  318. if (!sound_device) {
  319. dev_err(&pdev->dev, "platform device alloc failed\n");
  320. ret = -ENOMEM;
  321. goto error;
  322. }
  323. /* Associate the card data with the sound device */
  324. platform_set_drvdata(sound_device, &machine_data->card);
  325. /* Register with ASoC */
  326. ret = platform_device_add(sound_device);
  327. if (ret) {
  328. dev_err(&pdev->dev, "platform device add failed\n");
  329. goto error_sound;
  330. }
  331. dev_set_drvdata(&pdev->dev, sound_device);
  332. of_node_put(codec_np);
  333. return 0;
  334. error_sound:
  335. platform_device_put(sound_device);
  336. error:
  337. kfree(machine_data);
  338. error_alloc:
  339. of_node_put(codec_np);
  340. return ret;
  341. }
  342. /**
  343. * mpc8610_hpcd_remove: remove the platform device
  344. *
  345. * This function is called when the platform device is removed.
  346. */
  347. static int __devexit mpc8610_hpcd_remove(struct platform_device *pdev)
  348. {
  349. struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
  350. struct snd_soc_card *card = platform_get_drvdata(sound_device);
  351. struct mpc8610_hpcd_data *machine_data =
  352. container_of(card, struct mpc8610_hpcd_data, card);
  353. platform_device_unregister(sound_device);
  354. kfree(machine_data);
  355. sound_device->dev.platform_data = NULL;
  356. dev_set_drvdata(&pdev->dev, NULL);
  357. return 0;
  358. }
  359. static struct platform_driver mpc8610_hpcd_driver = {
  360. .probe = mpc8610_hpcd_probe,
  361. .remove = __devexit_p(mpc8610_hpcd_remove),
  362. .driver = {
  363. /* The name must match 'compatible' property in the device tree,
  364. * in lowercase letters.
  365. */
  366. .name = "snd-soc-mpc8610hpcd",
  367. .owner = THIS_MODULE,
  368. },
  369. };
  370. /**
  371. * mpc8610_hpcd_init: machine driver initialization.
  372. *
  373. * This function is called when this module is loaded.
  374. */
  375. static int __init mpc8610_hpcd_init(void)
  376. {
  377. struct device_node *guts_np;
  378. struct resource res;
  379. pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
  380. /* Get the physical address of the global utilities registers */
  381. guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
  382. if (of_address_to_resource(guts_np, 0, &res)) {
  383. pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
  384. return -EINVAL;
  385. }
  386. guts_phys = res.start;
  387. return platform_driver_register(&mpc8610_hpcd_driver);
  388. }
  389. /**
  390. * mpc8610_hpcd_exit: machine driver exit
  391. *
  392. * This function is called when this driver is unloaded.
  393. */
  394. static void __exit mpc8610_hpcd_exit(void)
  395. {
  396. platform_driver_unregister(&mpc8610_hpcd_driver);
  397. }
  398. module_init(mpc8610_hpcd_init);
  399. module_exit(mpc8610_hpcd_exit);
  400. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  401. MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
  402. MODULE_LICENSE("GPL v2");