1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Phytec pcm030 driver for the PSC of the Freescale MPC52xx
4 // configured as AC97 interface
5 //
6 // Copyright 2008 Jon Smirl, Digispeaker
7 // Author: Jon Smirl <jonsmirl@gmail.com>
8 
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/of_device.h>
13 #include <linux/of_platform.h>
14 
15 #include <sound/soc.h>
16 
17 #include "mpc5200_dma.h"
18 
19 #define DRV_NAME "pcm030-audio-fabric"
20 
21 struct pcm030_audio_data {
22 	struct snd_soc_card *card;
23 	struct platform_device *codec_device;
24 };
25 
26 static struct snd_soc_dai_link pcm030_fabric_dai[] = {
27 {
28 	.name = "AC97.0",
29 	.stream_name = "AC97 Analog",
30 	.codec_dai_name = "wm9712-hifi",
31 	.cpu_dai_name = "mpc5200-psc-ac97.0",
32 	.codec_name = "wm9712-codec",
33 },
34 {
35 	.name = "AC97.1",
36 	.stream_name = "AC97 IEC958",
37 	.codec_dai_name = "wm9712-aux",
38 	.cpu_dai_name = "mpc5200-psc-ac97.1",
39 	.codec_name = "wm9712-codec",
40 },
41 };
42 
43 static struct snd_soc_card pcm030_card = {
44 	.name = "pcm030",
45 	.owner = THIS_MODULE,
46 	.dai_link = pcm030_fabric_dai,
47 	.num_links = ARRAY_SIZE(pcm030_fabric_dai),
48 };
49 
50 static int pcm030_fabric_probe(struct platform_device *op)
51 {
52 	struct device_node *np = op->dev.of_node;
53 	struct device_node *platform_np;
54 	struct snd_soc_card *card = &pcm030_card;
55 	struct pcm030_audio_data *pdata;
56 	struct snd_soc_dai_link *dai_link;
57 	int ret;
58 	int i;
59 
60 	if (!of_machine_is_compatible("phytec,pcm030"))
61 		return -ENODEV;
62 
63 	pdata = devm_kzalloc(&op->dev, sizeof(struct pcm030_audio_data),
64 			     GFP_KERNEL);
65 	if (!pdata)
66 		return -ENOMEM;
67 
68 	card->dev = &op->dev;
69 
70 	pdata->card = card;
71 
72 	platform_np = of_parse_phandle(np, "asoc-platform", 0);
73 	if (!platform_np) {
74 		dev_err(&op->dev, "ac97 not registered\n");
75 		return -ENODEV;
76 	}
77 
78 	for_each_card_prelinks(card, i, dai_link)
79 		dai_link->platform_of_node = platform_np;
80 
81 	ret = request_module("snd-soc-wm9712");
82 	if (ret)
83 		dev_err(&op->dev, "request_module returned: %d\n", ret);
84 
85 	pdata->codec_device = platform_device_alloc("wm9712-codec", -1);
86 	if (!pdata->codec_device)
87 		dev_err(&op->dev, "platform_device_alloc() failed\n");
88 
89 	ret = platform_device_add(pdata->codec_device);
90 	if (ret)
91 		dev_err(&op->dev, "platform_device_add() failed: %d\n", ret);
92 
93 	ret = snd_soc_register_card(card);
94 	if (ret)
95 		dev_err(&op->dev, "snd_soc_register_card() failed: %d\n", ret);
96 
97 	platform_set_drvdata(op, pdata);
98 
99 	return ret;
100 }
101 
102 static int pcm030_fabric_remove(struct platform_device *op)
103 {
104 	struct pcm030_audio_data *pdata = platform_get_drvdata(op);
105 	int ret;
106 
107 	ret = snd_soc_unregister_card(pdata->card);
108 	platform_device_unregister(pdata->codec_device);
109 
110 	return ret;
111 }
112 
113 static const struct of_device_id pcm030_audio_match[] = {
114 	{ .compatible = "phytec,pcm030-audio-fabric", },
115 	{}
116 };
117 MODULE_DEVICE_TABLE(of, pcm030_audio_match);
118 
119 static struct platform_driver pcm030_fabric_driver = {
120 	.probe		= pcm030_fabric_probe,
121 	.remove		= pcm030_fabric_remove,
122 	.driver		= {
123 		.name	= DRV_NAME,
124 		.of_match_table    = pcm030_audio_match,
125 	},
126 };
127 
128 module_platform_driver(pcm030_fabric_driver);
129 
130 
131 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
132 MODULE_DESCRIPTION(DRV_NAME ": mpc5200 pcm030 fabric driver");
133 MODULE_LICENSE("GPL");
134 
135