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