mpc8610_hpcd.c 13 KB

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